Reputation: 41
Is it possible to send POST data to an URL and get the response back from that webpage, it's in JSON, and use that data for something different. If it is possible, how?
Thanks :)
EDIT: I have a form sending to another php script on another server, but i want to get the response from that script and use it in my script. The data send by the another is in JSON, but that isn't a problem anymore.
Upvotes: 1
Views: 6540
Reputation: 4321
using php curl you can achieve this as following
$url = "http://google.com/";
$aParameter = array('id'=>1,'name'=>'test'); //parameters to be sent
$params = json_encode($aParameter); //convert param to json string
//headers to be sent optional if no header required skip this and remove curlopt_httpheader thing from curl call
$aHeaders = array(
'Client-Secret'=>'XXXXX',
'Authorization'=>'xxxxxx',
'Content-Type'=>'Content-Type:application/json',
'accept'=>'accept:application/json'
);
$c = curl_init();
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0'); // empty user agents probably not accepted
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_AUTOREFERER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_HTTPHEADER, $aHeaders);
curl_setopt($c, CURLOPT_URL, $url );
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c,CURLOPT_POSTFIELDS,$params);
$sResponse[$key] = curl_exec($c);
Upvotes: 1