Question

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')

Was it helpful?

Solution

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);

OTHER TIPS

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top