Chu*_*utt 3 php api rest put httpwebrequest
对这个问题的新生事道歉.我正在考虑将一个网站的API集成到我自己的网站中.以下是他们文档中的一些引用:
目前我们只支持XML,在调用我们的API时,HTTP Accept头内容类型必须设置为"application/xml".
API使用PUT请求方法.
我有我想发送的XML,并且我有想要发送它的URL,但是如何在PHP中构建一个合适的HTTP请求,它还将获取返回的XML?
提前致谢.
ale*_*exn 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);
Run Code Online (Sandbox Code Playgroud)
这实际上对我有用:
$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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8207 次 |
| 最近记录: |