【问题标题】:Server Side script for cURL requestcURL 请求的服务器端脚本
【发布时间】: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[] 或任何曾经用于它的东西。

标签: php curl


【解决方案1】:

为了能够阅读它试试这个

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)

【讨论】:

  • 这是我在你的答案中的 2 个示例的输出 @Andy Gee Array ( ) 第一个我根本没有输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 2016-02-27
相关资源
最近更新 更多