HTTP请求与file_get_contents,获取响应代码 [英] HTTP requests with file_get_contents, getting the response code

查看:153
本文介绍了HTTP请求与file_get_contents,获取响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 file_get_contents 连同 stream_context_create 进行POST请求。我的代码到目前为止:

I'm trying to use file_get_contents together with stream_context_create to make POST requests. My code so far:

    $options = array('http' => array(
        'method'  => 'POST',
        'content' => $data,
        'header'  => 
            "Content-Type: text/plain\r\n" .
            "Content-Length: " . strlen($data) . "\r\n"
    ));
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

它工作正常,但是当发生HTTP错误时,会发出警告:

It works fine, however, when an HTTP error occurs, it spits out a warning:

file_get_contents(...): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

并返回false。有没有办法:

and returns false. Is there a way to:


  • 禁止警告(如果发生故障,我打算抛出自己的异常)

  • 从流中获取错误信息(至少是响应代码)

我知道如何做这个卷曲,我的问题具体是关于流/文件功能。

I know how to do that with curl, my question is specifically about stream/file functions.

对不起,如果这是rtm,流的东西对我来说是新的。

Sorry if this is rtm, the streams stuff is new to me.

推荐答案

http ://php.net/manual/en/reserved.variables.httpresponseheader.php

file_get_contents("http://example.com");
var_dump($http_response_header);

这篇关于HTTP请求与file_get_contents,获取响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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