2014-10-27 61 views
0

是否可以将POST数据发送到URL并从该网页获取响应,使用JSON,并将该数据用于不同的内容。 如果可能的话,怎么样?PHP发送数据到URL并获得该网页的响应

谢谢:)

编辑:我有一个表格发送到另一个PHP脚本另一台服务器上,但我想从该脚本的响应,并在我的脚本中使用它。另一个发送的数据是JSON,但这不再是问题。

+0

是有可能,例如谷歌jquery和ajax – donald123 2014-10-27 09:09:09

+0

是的,这是可能的。但是,如果能够解释代码所涉及的问题,那么它将帮助我们更好地理解您的问题。如果您不清楚POST请求的工作方式以及我们如何创建和发送JSON响应,我建议您使用Google搜索。你会得到很多有用的教程来帮助你入门。 – 2014-10-27 09:09:20

+0

对于PHP,请查看'cURL'函数。 – Paul 2014-10-27 09:09:55

回答

0

使用PHP卷曲,你可以做到这一点如下

$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); 
相关问题