Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP post to api, receive an image

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

like image 964
Nitsan Baleli Avatar asked Mar 28 '26 00:03

Nitsan Baleli


1 Answers

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);
like image 59
mleko Avatar answered Mar 29 '26 13:03

mleko