PHP HTTP POST Request


PHP HTTP POST Request

我需要使用以下设置通过HTTPPOST将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."

还没有足够的代表发表评论,所以我将把它作为答案发布。

PHP的cURL会自动发送Host(来自$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格式不正确,或者传递的参数不正确。