本文介绍了如何分析PHP中的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 file_get_contents
与 api 进行交互以处理简单的 GET
请求……但是有时它会抛出标头,表明存在错误.如何获取这些标题并确定是否存在问题?
I'm using a file_get_contents
to interact with an api for simple GET
requests... however sometimes it throws headers signifying there's been an error. How can I get these headers and determine if there's a problem?
推荐答案
使用 curl 而不是 file_get_contents.
Use curl instead of file_get_contents.
参见:http://www.php.net/manual/en/curl.examples-basic.php
我想如果您与 REST Api 通信,那么您实际上希望返回 Http 状态代码.在这种情况下,您可以执行以下操作:
I imagine if your communicating with a REST Api then your actaully wanting the Http Status code returned. In which case you could do something like this:
<?php
$ch = curl_init("http://www.example.com/api/users/1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 501) {
echo 'Ops it not implemented';
}
fclose($fp);
?>
这篇关于如何分析PHP中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!