2014-10-27 62 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); 
相關問題