Php cURL请求的服务器端脚本
我使用cURL,但直到现在我还使用它从服务器请求数据。但现在我想编写API,数据将通过cURL请求。但我不知道服务器如何从cURL请求中读取数据 这是我的“客户机-服务器”端请求:Php cURL请求的服务器端脚本,php,curl,Php,Curl,我使用cURL,但直到现在我还使用它从服务器请求数据。但现在我想编写API,数据将通过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_se
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;
函数sendRequest($site\u name、$send\u xml、$header\u type=array('Content-type:text/xml'))
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$site_name);
卷曲设置($ch,卷曲设置桩,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_头,0);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_超时,120);
$result=curl\u exec($ch);
返回$result;
}
$xml=”
示例用户
示例通行证
";
$sendreq=sendRequest(“http://sitename.com/example.php“,$xml);
echo$sendreq;
我需要如何编写“主服务器”端脚本,以便读取请求中的用户和传递信息???
非常感谢。为了能够阅读,请尝试以下内容
curl_setopt($ch, CURLOPT_POSTFIELDS,array('data'=>$send_xml));
然后
或者,跳过XML并尝试以下操作:
$data = array(
'request' => array(
'session' => array(
'user'=>'exampleuser',
'pass'=>'examplepass')
)
);
$sendreq = sendRequest("http://sitename.com/example.php",$data);
在example.php中
print_r($_POST)
PHP在服务器上执行。。。这毫无意义。@DigitalChris当我说client时,运行应用程序的是“client-server”,而server是“Main-server”,如果必须使用XML,那么就需要example.php中的XML解析器。如果您更灵活地使用HTTP post请求来避免XML解析器的开销。@AndyGee我需要使用XML,因为请求数据可能太长。我也可以解析请求数据,但我不知道如何读取它$_POST[]或$\u SERVER[]或其他任何用于它的东西。有关如何解析XML,请参阅您答案中的2个示例中的这是我的输出@Andy Gee Array(),这是我没有输出的第一个示例
print_r($_POST)