我试图做一个XML发布请求,期望使用curl响应来构建标题和内容。这是我的代码:PHP卷曲问题 - 试图发送XML发布请求
<?php
function post_xml($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml; charset=utf-8"));
curl_setopt($ch, CURLOPT_USERPWD, 'myusername:mypassword');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
$xml = '<?xml version="1.0" encoding="UTF-8"?><testing><test type="integer">1</test></testing>';
$url = "http://example.com/api/test.php";
$result = post_xml($url, $xml);
echo "<pre>"; print_r($result); echo "</pre>";
?>
要查看我的呼出请求我使用:
$info = curl_getinfo($ch);
echo($info['request_header']);
所发送的生成的HTTP请求是:
POST /api/test.php HTTP/1.1
Authorization: Basic pwmtJdCVwNEawdaODH
Host: example.com
Accept: */*
Content-Type: application/xml; charset=utf-8
Content-Length: 95
的内容长度为95,但是下面没有显示任何xml内容。这会导致422不可处理的实体错误。我完全错过了什么吗?我希望能够在发送的请求中看到XML。
问题必须与远程API;你的代码肯定是在移动数据。我用一个脚本测试它,它只会将原始postbody转储到测试文件并返回请求的散列,并且它工作得很好。 – Dereleased
等待!你在浏览器中查看这个吗?它可能会隐藏你的XML!试试这个,而不是print_r:'echo'
';' – Dereleased对不起,我发布的请求是原始请求。我认为这可能是我尝试使用的API的一个问题,所以我会与他们联系。这一直都让我坚持了整整一个早上,但我完全遵循他们的文档。 – Brian