I've used code below to send XML to my REST API. $xml_string_data contains proper XML, and it is passed well to mypi.php:
我使用下面的代码将XML发送到我的REST API。 $ xml_string_data包含正确的XML,并且很好地传递给mypi.php:
//set POST variables
$url = 'http://www.server.cu/mypi.php';
$fields = array(
'data'=>urlencode($xml_string_data)
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
echo $fields_string;
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
"Expect: "
));
//execute post
$result = @curl_exec($ch);
But when I've added other field:
但是当我添加其他字段时:
$fields = array(
'method' => "methodGoPay",
'data'=>urlencode($xml_string_data)
);
It stopped to work. On the mypi.php I don't recieve any more POST parameters at all!
它停止了工作。在mypi.php上我根本没有收到任何POST参数!
Could you you please tell me what to do to send XML and other post parameters in one cURL request?
请问您能告诉我如何在一个cURL请求中发送XML和其他帖子参数?
Please don't suggest using any libraries, I wan't to acomplish it in plain PHP.
请不要建议使用任何库,我不想用普通的PHP来完成它。
1 个解决方案
#1
3
I don't see anything wrong with this script. It's most likely an issue with mypi.php.
我没有看到这个脚本有什么问题。这很可能是mypi.php的一个问题。
You do have an extra & at the end. Maybe that confuses the server? The rtrim doesn't change the $field_string and it returns the trimmed string.
你确实有一个额外的&最后。也许这会混淆服务器? rtrim不会更改$ field_string并返回修剪后的字符串。
The postfields can be simplified like this,
后场可以这样简化,
$fields = array(
'method' => "methodGoPay",
'data'=> $xml_string_data // No encode here
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
#1
3
I don't see anything wrong with this script. It's most likely an issue with mypi.php.
我没有看到这个脚本有什么问题。这很可能是mypi.php的一个问题。
You do have an extra & at the end. Maybe that confuses the server? The rtrim doesn't change the $field_string and it returns the trimmed string.
你确实有一个额外的&最后。也许这会混淆服务器? rtrim不会更改$ field_string并返回修剪后的字符串。
The postfields can be simplified like this,
后场可以这样简化,
$fields = array(
'method' => "methodGoPay",
'data'=> $xml_string_data // No encode here
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));