状态
我正在尝试发送http请求,但我注意到当我尝试将PHPSESSID
作为COOKIE发送时,我收到警告:
file_get_contents('http://mysite.localhost'):无法打开流:HTTP 请求失败
代码:
$options = array(
'http' => array(
'header' =>
"Content-type: application/x-www-form-urlencodedrn".
"Cookie: {$this->COOKIE}rn",
'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"
我做错了什么?
必须先
关闭当前会话,就在调用函数"file_get_contents"之前调用此函数"session_write_close"。
如果要在调用"file_get_contents"后使用会话,请不要忘记在调用"后重新打开session_start。
所以代码将是这样的:
$options = array(
'http' => array(
'header' =>
"Content-type: application/x-www-form-urlencodedrn".
"Cookie: {$this->COOKIE}rn",
'method' => $this->method,
'content' => $this->POST,
),
);
session_write_close();
$c = file_get_contents($this->uri, false, stream_context_create($options));
请确保 allow-url-fopen 在 php.ini 配置中处于"打开"