cURL请求的服务器端脚本

时间:2014-01-13 00:41:34

标签: php curl

我使用cURL,但直到现在我用它来从服务器请求数据。但现在我想要写入API,并且将使用cURL请求数据。但我不知道Server如何从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 个答案:

答案 0 :(得分: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)