Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP发出HTTP/1.1请求_Php_Http - Fatal编程技术网

使用PHP发出HTTP/1.1请求

使用PHP发出HTTP/1.1请求,php,http,Php,Http,我的代码正在使用file\u get\u contents()向API端点发出get请求。看起来它正在使用HTTP/1.0,我的系统管理员说我需要使用HTTP/1.1。如何发出HTTP/1.1请求?我需要使用卷发还是有更好/更简单的方法 更新 我决定使用cURL,因为我使用的是PHP5.1.6。我最终通过以下操作强制使用HTTP/1.1: curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 如果我使用5.3或更高版本,我

我的代码正在使用
file\u get\u contents()
向API端点发出get请求。看起来它正在使用
HTTP/1.0
,我的系统管理员说我需要使用
HTTP/1.1
。如何发出
HTTP/1.1
请求?我需要使用卷发还是有更好/更简单的方法

更新 我决定使用cURL,因为我使用的是PHP5.1.6。我最终通过以下操作强制使用HTTP/1.1:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
如果我使用5.3或更高版本,我会尝试这样做:

$ctx = stream_context_create(array(
    'http' => array('timeout' => 5, 'protocol_version' => 1.1)
    ));
$res = file_get_contents($url, 0, $ctx);
echo $res;
# Connect to the Web API using cURL.
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/api.php?123=456'); 
curl_setopt($ch, CURLOPT_TIMEOUT, '3'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$xmlstr = curl_exec($ch); 
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

注意:5.3.0之前的PHP不支持 实现分块传输解码。 如果此值设置为1.1,则为 遵守1.1的责任


我发现的另一个可能提供HTTP/1.1的选项是使用

,在这两种情况下,我都会使用cURL,它提供了更多的控制,特别是它提供了超时选项。这在调用外部API时非常重要,以免在远程API关闭时应用程序冻结

我可以这样说:

$ctx = stream_context_create(array(
    'http' => array('timeout' => 5, 'protocol_version' => 1.1)
    ));
$res = file_get_contents($url, 0, $ctx);
echo $res;
# Connect to the Web API using cURL.
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/api.php?123=456'); 
curl_setopt($ch, CURLOPT_TIMEOUT, '3'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$xmlstr = curl_exec($ch); 
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

cURL将默认使用HTTP/1.1,除非您使用
cURL\u setopt($s,CURLOPT\u HTTPHEADER,$headers)指定其他内容,其中$headers是一个数组

其他想要使用流上下文\u创建/file\u获取\u内容的人知道,如果您的服务器配置为使用保持活动连接,响应将不会返回任何内容,您需要添加
'protocol\u version'=>1.1
以及
'header'=>'Connection:close'


下面的示例:

$ctx = stream_context_create(array(
               'http' => array(
                  'timeout' => 5,
                  'protocol_version' => 1.1,
                  'header' => 'Connection: close'
                )
              ));
$res = file_get_contents($url, 0, $ctx);