我試圖做一個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