2

trying to send post request to api, to get an image back.

example url:

https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=1003123&format=png&cloudid=asdasdasd&pass=123123123 

the above url works fine in the browser,
the api doesnt care if the request is get/post,

result of my code is always 'invalid input'.

code:

$url='https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$u = rand();
$p = rand();

$fields = array(
    'user'=> urlencode($u),
    'pass'=> urlencode($p),
    'format'=> urlencode('jpg'), 
    'cloudid' => urlencode('test')
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$output = curl_exec($ch);

curl_close($ch);

echo $output;

on a side note: is there a way to debug the request in order to see what is being sent ?

11
  • 1
    The URL you provided is in GET request format, so if you are directly pasting that URL in browser then it will make GET request only. But in your code you have used POST it seems. Commented Apr 24, 2014 at 11:53
  • yes, I've mentioned it to make it clear that the API does return an image on a post request. tested with hurl.it Commented Apr 24, 2014 at 11:55
  • Ok, But I tried using Advance Rest Client it isn't working with POST request. Only for GET request its working. Commented Apr 24, 2014 at 11:58
  • @Log1c Please check again since it's working OK for me ? Commented Apr 24, 2014 at 12:01
  • Not working dude. FYI while testing for POST you should't pass parameters in URL. here is my test result webpagescreenshot.info/i3/5358fe38d31971-20758372 Commented Apr 24, 2014 at 12:06

1 Answer 1

1

The URL provided isn't working for POST request. Here is resulting screenshot (I tried using Advance Rest Client)

enter image description here

However Its working perfectly with GET method. So you can continue using GET request method to generate QR code.

I agree that GET isn't much secure compare to POST method but in your case while requesting from curl user won't get to know about such URL parameters (userid, password). Because curl request will be sending from your web server and not from client/user's browser.

Later you can just output the response image you got from the api.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.