制作PHP中的REST API请求 [英] Making a REST API request in PHP
问题描述
有关这个问题的newbishness道歉。我在寻找到一个网站的API集成到自己的网站。下面是从他们的文档一些报价:
Apologies for newbishness of this question. I'm looking into integrating one website's API into my own website. Here's some quotes from their documentation:
目前,我们只支持XML,
拨打我们的API时的HTTP接受
标题内容类型必须设置为
应用程序/ XML。
At the moment we only support XML, when calling our API the HTTP Accept header content type must be set to "application/xml".
该API使用的 PUT 请求方法。
The API uses the PUT request method.
我有我想要发送的XML,我有我想将它发送到URL,但我要如何去构建在PHP合适的HTTP请求,也将抢得的返回的XML?
I have the XML I want to send, and I have the URL I want to send it to, but how do I go about constructing a suitable HTTP Request in PHP that will also grab the XML that's returned?
先谢谢了。
推荐答案
这实际上是对我工作:
$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);
}
这篇关于制作PHP中的REST API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!