Heim  >  Artikel  >  Backend-Entwicklung  >  php file_get_content 怎么获取错误信息?

php file_get_content 怎么获取错误信息?

WBOY
WBOYOriginal
2016-06-06 20:20:331784Durchsuche

使用php做跨域代理,使用file_get_content函数发出get请求的时候,对方服务器设置的是,如果验证错误,则会引发http 422,然后还会返回一个json值。

请问,怎么获取这个json值,因为该函数直接返回建立链接失败,没有任何值。

回复内容:

使用php做跨域代理,使用file_get_content函数发出get请求的时候,对方服务器设置的是,如果验证错误,则会引发http 422,然后还会返回一个json值。

请问,怎么获取这个json值,因为该函数直接返回建立链接失败,没有任何值。

参考 (可能需要搭梯子)

<code>set_error_handler(
    create_function(
        '$severity, $message, $file, $line',
        'throw new ErrorException($message, $severity, $severity, $file, $line);'
    )
);

try {
    file_get_contents('www.google.com');
}
catch (Exception $e) {
    echo $e->getMessage();
}

restore_error_handler();</code>

另外建议使用curl.

有网址, 可以测试吗?

返回 false ? 试下访问百度啥的能不能访问的通

这应用场景好像还没有碰到过,为嘛要返回422呢,有返回值就应该是200,然后对json判断,试试curl

这个函数不行吧,你用curl系列函数试试

<code class="php">function get_contents()
{
    $content = @file_get_contents("http://segmentfault.com/q/101000000427840111");
    var_dump($http_response_header);
    var_dump($content);
}
get_contents();</code>

返回的http 状态码可以这么获取
但是如果返回 非 200的话,file_get_contents 会返回 false
可以判断不同的 http status code ,进行不同的逻辑处理

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn