如何分析在PHP头 [英] How to analyze headers in PHP

查看:130
本文介绍了如何分析在PHP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是的file_get_contents 与简单 GET 请求的API进行交互...但是有时它抛出头标志着还有的是一个错误。我怎样才能获得这些报头,并确定是否有问题?

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?

推荐答案

使用卷曲的file_get_contents代替

Use curl instead of file_get_contents.

请参阅: http://www.php.net/manual/ EN / curl.examples-basic.php

我想如果用REST API通信那么你想actaully HTTP状态code返回。在这种情况下,你可以做这样的事情:

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头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆