1

I am trying to form the following CURL post request through PHP? How do i do it, all of what i tried came back as invalid post parameters.

This is what i am trying to POST through php (i have the API_KEY, api_sig.. but dont know how put below post query through in PHP):

curl -d "api_key=KEY&sig=SIGNATURE&time_stamp=20&json=1" \ http://api.i.com/v1/update/

ChicagoDude
  • 591
  • 7
  • 21
  • Looks like a direct shell call to curl. Take a look at a [basic curl example](http://us.php.net/manual/en/curl.examples-basic.php) using PHP's curl library. You'd send data pairs as [`CURLOPT_POSTFIELDS`](http://us.php.net/manual/en/function.curl-setopt.php). – Wiseguy Dec 07 '11 at 05:44
  • $ch = curl_init("http://api.iq.com/v1/update/"); $fp = fopen("response.txt", "d"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); Like above? I am not sure how to do it in PHP - hence my question :) – ChicagoDude Dec 07 '11 at 05:48

3 Answers3

5

This should work

$url = "http://api.iq.com/v1/update/";
$data = "api_key=API_KEY&api_sig=SIGNATURE&time_stamp=20090612111832&json=1";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); /* obey redirects */
curl_setopt($ch, CURLOPT_HEADER, 0);  /* No HTTP headers */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  /* return the data */

$result = curl_exec($ch);

curl_close($ch);
useful
  • 472
  • 3
  • 6
0

Please refer to following PHP code

 define('POSTURL', 'http://api.iq.com/v1/update/');
 define('POSTVARS', 'api_key=API_KEY&api_sig=SIGNATURE&time_stamp=20090612111832&json=1');  //removed typo
 $ch = curl_init(POSTURL);
 curl_setopt($ch, CURLOPT_POST  ,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS ,POSTVARS);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL`
 $Rec_Data = curl_exec($ch);`
 curl_close($ch);
VMai
  • 10,156
  • 9
  • 25
  • 34
0

Put everything in one line:

curl -d "api_key=API_KEY&api_sig=SIGNATURE&time_stamp=20090612111832&json=1" http://api.iq.com/v1/update/

Use exec() to run it, for example

exec("curl -d \"api_key=API_KEY&api_sig=SIGNATURE&time_stamp=20090612111832&json=1\" http://api.iq.com/v1/update/", $results);

$results holds the returned data.

subroutines
  • 1,458
  • 1
  • 12
  • 16