Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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 为什么在使用stream\u get\u内容时,我的授权标题会消失?_Php - Fatal编程技术网

Php 为什么在使用stream\u get\u内容时,我的授权标题会消失?

Php 为什么在使用stream\u get\u内容时,我的授权标题会消失?,php,Php,我正在尝试使用PHP的stream\u get\u contents机制发出请求,因为我没有CURL。此请求在本地运行良好,但部署到远程服务器时不会发送所有头。以下是PHP代码片段: <?php $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => array( 'Content-typ

我正在尝试使用PHP的
stream\u get\u contents
机制发出请求,因为我没有CURL。此请求在本地运行良好,但部署到远程服务器时不会发送所有头。以下是PHP代码片段:

<?php
$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => array(
            'Content-type: application/json',
            'Authorization: Basic '.base64_encode('username:secret'),
        ),
        'content' => json_encode(array(
            'key' => 'value',
        )),
    )
));
file_get_contents('http://requestb.in/xxxxxx', false, $context);
?>
…下面是我从远程服务器执行代码片段时得到的信息

User-Agent: PHP/5.2.17
Host: requestb.in
Via: 1.1 vegur
X-Request-Id: 14e7af5f-39aa-474a-8d1a-af992997d0ef
Content-Length: 70
Total-Route-Time: 0
Content-Type: application/x-www-form-urlencoded
Accept: */*
Connect-Time: 7
Connection: close
内容长度不同,因为我修改了pOST请求的有效负载,但正如您所看到的,在第二个场景中,我无法获得
授权
标题,并且
内容类型
标题也发生了更改


我真的很迷茫,还没有找到这种古怪行为的解释。(我使用RequestBin只是为了帮助我解决问题。)

上下文数组中的
'header'
键必须是字符串。请参见中的示例


如果php是使用标志--“with curlwrappers”编译的,那么这个问题可能是由于php中的错误造成的。
您的主要问题是缺少授权标头吗?我遇到了一个类似的问题,通过如下编辑.htaccess文件得到了解决:


    RewriteEngine on
    RewriteCond %{HTTP:Authorization} ^(.)
    RewriteRule . - [e=HTTP_AUTHORIZATION:%1]
重新启动发动机 重写条件%{HTTP:Authorization}^(.) 重写规则[e=HTTP\U授权:%1]

我两种都试过了,但似乎都不起作用。正如您所看到的,服务器正在运行的PHP版本是5.2.17,并且从5.2.10开始,
头可以是一个数字索引数组,正如您从这里的更改日志中看到的那样,索引数组从未对我起作用。我用我在一个项目中使用的一些代码编辑了我的答案。我试过你的代码片段,但标题仍然被去掉了。我看不到
授权
标题。我忘了提到这段代码在另一台服务器上按预期工作,但是这个特定的服务器删除了头。在这两种情况下,您是通过PHP运行的吗?我不是通过CLI运行的(如果您是指CLI),而是在Apache上运行的PHP脚本。
$method = 'GET';
$contextOptions = array(
    'http'  => array(
        'method'    => $method,
        'header'    => array(
            'X-Forwarded-For: ' . $_SERVER['REMOTE_ADDR'],
            'Authorization: Basic ' . base64_encode($apiKey . ':' . $flags),
        ),
    )
);

if ('POST' === $method || 'GET' === $method) {
    $contextOptions['http']['header'][] = 'Content-Type: application/x-www-form-urlencoded';
} elseif ('PUT' === $method || 'UPDATE' === $method) {
    $contextOptions['http']['header'][] = 'Content-Type: application/octet-stream';
}

$contextOptions['http']['header'][] = 'Content-Length: ' . strlen($data);
$contextOptions['http']['content'] = $data;
$contextOptions['http']['header'] = implode("\r\n", $contextOptions['http']['header']);

    RewriteEngine on
    RewriteCond %{HTTP:Authorization} ^(.)
    RewriteRule . - [e=HTTP_AUTHORIZATION:%1]