【问题标题】:Cannot send 'PHPSESSID' via file_get_contents && stream_context_create无法通过 file_get_contents && stream_context_create 发送“PHPSESSID”
【发布时间】:2013-09-07 19:27:00
【问题描述】:

我正在尝试发送一个 http 请求,但我注意到当我尝试将 PHPSESSID 作为 COOKIE 发送时,我收到了警告:

file_get_contents('http://mysite.localhost'):打开流失败:HTTP 请求失败

代码

$options = array(
    'http' => array(
        'header'  => 
            "Content-type: application/x-www-form-urlencoded\r\n".
            "Cookie: {$this->COOKIE}\r\n",
                'method'  => $this->method,
                'content' => $this->POST,
            ),
        );
$c = file_get_contents($this->uri, false, stream_context_create($options));
if(!$c)
    throw new \Exception("Unable to fetch the request...");
echo $c;

已编辑
var_dump 输出:

var_dump($this->COOKIE, $this->method, $this->POST);

string(53) "HI=COOKIE; L=S; PHPSESSID=o1eqt811isceh4f0nhfnnlnm70"
string(4) "POST"
string(6) "POST=VALUE"

我做错了什么?

【问题讨论】:

    标签: php stream http-headers httpcookie


    【解决方案1】:

    您必须先关闭当前会话,然后在调用函数“file_get_contents”之前调用此函数“session_write_close”。

    如果您想在调用“file_get_contents”后使用会话,请不要忘记重新打开 session_start。

    所以代码会是这样的:

    $options = array(
        'http' => array(
            'header'  => 
                "Content-type: application/x-www-form-urlencoded\r\n".
                "Cookie: {$this->COOKIE}\r\n",
                    'method'  => $this->method,
                    'content' => $this->POST,
                ),
            );
    session_write_close();
    $c = file_get_contents($this->uri, false, stream_context_create($options));
    

    【讨论】:

      【解决方案2】:

      确保allow-url-fopen 在您的 php.ini 配置中处于“开启”状态

      【讨论】:

      • hmm,你能给我们($this->COOKIE, $this->method, $this->POST)的var_dump吗
      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2011-02-16
      • 2014-07-23
      • 2014-10-10
      • 1970-01-01
      • 2017-06-29
      • 2012-08-19
      • 2013-06-07
      相关资源
      最近更新 更多