2014-01-13 209 views
0

我使用cURL,但直到現在我用它從服務器請求數據。但是現在我想要寫入API並且將用cURL請求數據。但我不知道Server如何從cURL請求中讀取數據。cURL請求的服務器端腳本

這是我的「客戶端服務器」端請求:

function sendRequest($site_name,$send_xml,$header_type=array('Content-Type: text/xml')) 
{ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$site_name); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$send_xml); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_HTTPHEADER,$header_type); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
$result = curl_exec($ch); 
return $result; 
} 


$xml = "<request> 
<session> 
<user>exampleuser</user> 
<pass>examplepass</pass> 
</session> 
</request>"; 
$sendreq = sendRequest("http://sitename.com/example.php",$xml); 
echo $sendreq; 

如何做我需要寫「主服務器」端腳本,所以我可以讀到用戶和請求通過的??? 非常感謝。

+1

PHP在服務器上執行......這是沒有意義的。 –

+0

@DigitalChris當我說客戶端是「客戶端服務器」,運行應用程序,服務器是「主服務器」 – mandza

+0

如果您必須使用XML,那麼您需要在example.php中使用XML解析器。如果您更靈活地使用HTTP發佈請求來避免XML解析器的開銷。 –

回答

0

只是能夠閱讀它嘗試這種

curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml)); 

然後

print_r($_POST['data']) 

或者跳過XML和嘗試是這樣的:

$data = array(
    'request' => array(
     'session' => array(
      'user'=>'exampleuser', 
      'pass'=>'examplepass') 
     ) 
    ); 


$sendreq = sendRequest("http://sitename.com/example.php",$data); 

在使用example.php

print_r($_POST) 
+0

這是我的輸出2例在你的答案@Andy Gee 陣列 ( ) 第一個我沒有輸出 – mandza