2014-01-13 58 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