3

Trying to post to a url, and recieve an image.
for example (this works in the browser):

https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=123&pass=321&cloudid=test&format=png

my code:

$url = 'https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$fields = array(
  'user'=> 123,
  'pass'=> 321,
  'cloudid'=> 'test',
  'format'=> 'png'
);

$options = array(
    'http' => array(
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'method'  => 'POST',
    'content' => http_build_query($fields)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

tried following this answer.

returns "invalid input"

**EDIT**

also trying with curl:

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec( $ch );
var_dump($response);

same result ('invalid input')

10
  • The link you indicated is a GET, not a POST. Try changing your method to GET and see what happens. Commented Apr 23, 2014 at 10:07
  • 2
    Does the API require you to use HTTP GET or POST for the request? The link example would be using GET but the PHP code is using POST Commented Apr 23, 2014 at 10:08
  • 1
    Trying at hurl.it, it works for me, using both GET and POST Commented Apr 23, 2014 at 10:09
  • 1
    I would suggest using cURL for this. Commented Apr 23, 2014 at 10:12
  • 1
    Does it work when you put ?cloudid=test in the url (instead of sending it through $options['content'])= Commented Apr 23, 2014 at 10:21

2 Answers 2

1

Check this

You should add parameters to query instead of context which is not necessary at all

$url = 'https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$fields = array(
  'user'=> 123,
  'pass'=> 321,
  'cloudid'=> 'test',
  'format'=> 'png'
);

$result = file_get_contents($url."?".http_build_query($fields));
var_dump($result);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, maybe you can explain the logic of not having to provide header/method etc .. ?
You posted link which is working. Links are GET request. GET is default method, so there is no need to specify it. In GET request you can't have content, so there is no reason to specify it's type either.
0

You can use JQuery to send a post request and then take the respons via Jquery

  • Add jquery library to the html file
  • Create a simple html form with a button
  • And try bellow code

<input type="button" name="bttnLoadQR" onClick="
$.post( "https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php", 
{ user: "123", pass: "321", cloudid: "test", format: "png" },
function(data){
  $('#result').html(data);
});
">
<div id="result"></div>

Check here http://jsfiddle.net/S8Lgp/212/

1 Comment

A ok. You need only PHP Code for some php api?

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.