我正在尝试发送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;
EDITED
var_dump输出:
var_dump($this->COOKIE, $this->method, $this->POST);
IS
string(53) "HI=COOKIE; L=S; PHPSESSID=o1eqt811isceh4f0nhfnnlnm70"
string(4) "POST"
string(6) "POST=VALUE"
我做错了什么?
答案 0 :(得分:3)
您必须先关闭当前会话,在调用函数“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));
答案 1 :(得分:0)
确保php.ini配置中的allow-url-fopen“打开”