【问题标题】:PHP HTTP POST RequestPHP HTTP POST 请求
【发布时间】:2015-06-01 09:34:48
【问题描述】:

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

【问题讨论】:

  • 但是 $response 打印了什么?也许您可以像他说的那样打印错误(使用 curl_error()):stackoverflow.com/a/1234556/1919749
  • 我认为您不应该在$headers 中添加POST 行。不是header,是HTTP请求,是cURL基于CURLOPT_POST自动完成的。
  • 我现在已经更改了 POST 行,我现在得到这个响应...“无法转换为 System.Byte。参数名称:类型 ---> 输入字符串不正确格式。”

标签: php curl http-headers http-post


【解决方案1】:

没有足够的代表发表评论,所以我会发布这个作为答案。

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 格式不正确,或者作为不正确的参数传递。

【讨论】:

  • 响应“对象引用未设置为对象的实例。”
  • 是的,这些是来自远程网络服务器的错误;我帮不上这些。祝你好运!
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 2011-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多