PHP HTTP POST请求

时间:2015-03-27 22:21:48

标签: php curl http-headers http-post

我需要使用以下设置通过HTTP POST将XML字符串发送到另一台服务器...

POST /xmlreceive.asmx/CaseApplicationZipped HTTP/1.1
Host: www.dummyurl.co.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

XMLApplication=XMLstring&byArray=base64string

我猜我需要通过cURLfsockopen进行设置。

我已经尝试了以下但是没有任何运气让它发挥作用。

$url = "http://www.dummyurl.co.uk/XMLReceive.asmx/CaseApplicationZipped";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"//,
);

$post = http_build_query(array('XMLApplication' => $XML, 'byArray' => $base64));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);

echo "response: ".$response;

远程服务器提供以下响应......

"Object reference not set to an instance of an object."

1 个答案:

答案 0 :(得分:1)

还没有足够的回复评论,所以我会将此作为答案发布。

PHP的cURL会自动发送主机(来自$url)和Content-Length标头,因此您无需手动指定。

有一个方便的功能可用于构建$post字符串:http_build_query。它将正确处理您的POST正文的编码。它看起来像

$post = http_build_query(array('XMLApplication' => $XML, 'byArray' => $base64));

如果您要注销收到的标题,请查看curl_getopt功能。

至于您收到的错误,您似乎正在通过远程网站它没有预料到的事情。我不能代表您传递的内容或网站的期望,但Input string was not in a correct format似乎暗示您的$ XML格式不正确,或者传递的不正确参数。

相关问题