对这个问题的新生事道歉。我正在考虑将一个网站的API集成到我自己的网站中。以下是他们文档中的一些引用:
目前我们只支持XML, 在调用我们的API时,HTTP Accept 标头内容类型必须设置为 “应用程序/ xml”的。
API使用 PUT 请求方法。
我有要发送的XML,并且我有要发送的URL,但是如何在PHP中构建一个合适的HTTP请求,它还会获取返回的XML?
提前致谢。
答案 0 :(得分:12)
您可以使用file_get_contents和stream_context_create创建请求并阅读回复。这样的事情会做到:
$opts = array(
"http" => array(
"method" => "PUT",
"header" => "Accept: application/xml\r\n",
"content" => $xml
)
);
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
答案 1 :(得分:6)
这实际上对我有用:
$fp = fsockopen("ssl://api.staging.example.com", 443, $errno, $errstr, 30);
if (!$fp)
{
echo "<p>ERROR: $errstr ($errno)</p>";
return false;
}
else
{
$out = "PUT /path/account/ HTTP/1.1\r\n";
$out .= "Host: api.staging.example.com\r\n";
$out .= "Content-type: text/xml\r\n";
$out .= "Accept: application/xml\r\n";
$out .= "Content-length: ".strlen($xml)."\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $xml;
fwrite($fp, $out);
while (!feof($fp))
{
echo fgets($fp, 125);
}
fclose($fp);
}