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

Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52
  • The link you indicated is a GET, not a POST. Try changing your method to GET and see what happens. – Aleks G Apr 23 '14 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` – AeroX Apr 23 '14 at 10:08
  • 1
    Trying at http://www.hurl.it/, it works for me, using both `GET` and `POST` – leo Apr 23 '14 at 10:09
  • GET and POST gets the same result – Nitsan Baleli Apr 23 '14 at 10:11
  • 1
    I would suggest using [cURL](http://www.php.net/manual/en/book.curl.php) for this. – jonnu Apr 23 '14 at 10:12
  • if you POST something to an api it actually is the same as GET if the api isn't correctly setted up the url will show the POSTed information... so technically it's not POST/GET issue. – Mike M. Apr 23 '14 at 10:12
  • I updated the question with curl code, that gives the same result. – Nitsan Baleli Apr 23 '14 at 10:17
  • 1
    Does it work when you put `?cloudid=test` in the url (instead of sending it through `$options['content']`)= – leo Apr 23 '14 at 10:21
  • I couldn't catch any headers from your url. And than I realized it's https, hot http link. I don't know it it matters buy you are using `'http' => array(...` – CoR Apr 23 '14 at 10:27
  • @leo your answer is correct BTW – Nitsan Baleli Apr 23 '14 at 14:18

2 Answers2

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);
mleko
  • 11,650
  • 6
  • 50
  • 71
  • Thanks, maybe you can explain the logic of not having to provide header/method etc .. ? – Nitsan Baleli Apr 23 '14 at 10:36
  • 1
    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. – mleko Apr 23 '14 at 10:39
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/

A. Zalonis
  • 1,599
  • 6
  • 26
  • 41