cURL请求的服务器端脚本
问题描述:
我使用cURL,但直到现在我使用它从服务器请求数据。但现在我想要ot写API和数据将请求与cURL。但我不知道Server如何从cURL请求中读取数据。
I use cURL but untill now I used it for requesting data from servers. But now I want ot write API and data will be requested with cURL. But I don't know how Server reads data from cURL request.
这是我的客户端服务器端请求:
This is my "client server" side request:
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;
我如何需要写主服务器端脚本,所以我可以读取什么用户和传递请求是?
非常感谢。
How do I need to write "main server" side script so I can read what user and pass from request are??? Thank you a lot.
答
只要能读就可以了。
curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml));
然后
print_r($_POST['data'])
像这样:
$data = array(
'request' => array(
'session' => array(
'user'=>'exampleuser',
'pass'=>'examplepass')
)
);
$sendreq = sendRequest("http://sitename.com/example.php",$data);
在example.php
In example.php
print_r($_POST)