【发布时间】:2014-01-13 00:41:33
【问题描述】:
我使用 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;
我需要如何编写“主服务器”端脚本,以便我可以读取用户并从请求中传递? 非常感谢。
【问题讨论】:
-
PHP 是在服务器上执行的……这没有意义。
-
@DigitalChris 当我说客户端是运行应用程序的“客户端服务器”而服务器是“主服务器”时
-
如果您必须使用 XML,那么您需要在 example.php 中使用 xml 解析器。如果您更灵活地使用 HTTP 发布请求来避免 XML 解析器的开销。
-
@AndyGee 我需要使用 XML,因为请求数据可能太长。我也可以解析请求数据,但我只是不知道如何阅读它。 $_POST[] 或 $_SERVER[] 或任何曾经用于它的东西。