卷曲或 file_get_contents 不工作
curl or file_get_contents not working
我在发送帖子请求时遇到了一些问题。
我使用此脚本执行页面 http://localname.local/test
,页面 http://localname.local/directory/page.php
获得 json 数据。
$url = "http://localname.local/directory/page.php";
$post = [
"key1" => "hello",
"key2" => 885,
];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post),
'timeout' => 10
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ( $result === false ) {
// Handle error
return null;
}
else
return $result;
但 10 秒后,脚本收到消息:
Warning: file_get_contents(http://localname.local/directory/page.php): failed to open stream: HTTP request failed! in D:\ ... \html\test.php on line X
page.php 有效,我可以用我的浏览器作为客户端发送帖子请求,但 php(或 wamp)无法访问或向其自己的页面发送请求。
我在 wamp 3.0.9 上安装了 PHP 7.1.7、Apache 2.4.23 并且选项 allow_url_fopen
已启用。
编辑:
对于 CURL
public static function get_content($post)
{
$url = "http://localname.local/directory/page.php";
$query = http_build_query($post);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // Tell cURL that it should only spend X seconds trying to connect to the URL in question.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); // A given cURL operation should only take X seconds max.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // returns data to function
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
$data = curl_exec($curl);
if ( curl_errno($curl) )
throw new Exception(curl_error($curl));
curl_close($curl);
return $data;
}
得到
Fatal error: Uncaught Exception: Operation timed out after 10000 milliseconds with 0 bytes received in D:\ ... \html\test.php on line X
Exception: Operation timed out after 10000 milliseconds with 0 bytes received
为了 file_get_contents() 打开 URL,您必须在 php.ini 文件中启用设置 allow_url_fopen。
出于安全考虑,我建议您使用 cURL:
来实现您正在做的事情
function get_content($url,$post){
$query = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // returns data to function
curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
$data = curl_exec($ch);
// if you want to display error messages, do it before curl_close()
//echo curl_errno($ch); // if you want to display error number
//echo curl_error($ch); // if you want to display error message
curl_close($ch);
return $data;
}
echo get_content('http://localname.local/directory/page.php');
我认为你需要增加CURLOPT_TIMEOUT时间
真正的问题
test.php
使用 session_start()
test.php
在 page.php
上启动 POST request
page.php
使用 session_start()
<<< 冻结
我们不能同时使用 2 个会话 PHP。会话正在锁定并创建无限循环。
谢谢大家帮我找到解决办法。
我在发送帖子请求时遇到了一些问题。
我使用此脚本执行页面 http://localname.local/test
,页面 http://localname.local/directory/page.php
获得 json 数据。
$url = "http://localname.local/directory/page.php";
$post = [
"key1" => "hello",
"key2" => 885,
];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post),
'timeout' => 10
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ( $result === false ) {
// Handle error
return null;
}
else
return $result;
但 10 秒后,脚本收到消息:
Warning: file_get_contents(http://localname.local/directory/page.php): failed to open stream: HTTP request failed! in D:\ ... \html\test.php on line X
page.php 有效,我可以用我的浏览器作为客户端发送帖子请求,但 php(或 wamp)无法访问或向其自己的页面发送请求。
我在 wamp 3.0.9 上安装了 PHP 7.1.7、Apache 2.4.23 并且选项 allow_url_fopen
已启用。
编辑: 对于 CURL
public static function get_content($post)
{
$url = "http://localname.local/directory/page.php";
$query = http_build_query($post);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // Tell cURL that it should only spend X seconds trying to connect to the URL in question.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); // A given cURL operation should only take X seconds max.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // returns data to function
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
$data = curl_exec($curl);
if ( curl_errno($curl) )
throw new Exception(curl_error($curl));
curl_close($curl);
return $data;
}
得到
Fatal error: Uncaught Exception: Operation timed out after 10000 milliseconds with 0 bytes received in D:\ ... \html\test.php on line X
Exception: Operation timed out after 10000 milliseconds with 0 bytes received
为了 file_get_contents() 打开 URL,您必须在 php.ini 文件中启用设置 allow_url_fopen。
出于安全考虑,我建议您使用 cURL:
来实现您正在做的事情function get_content($url,$post){
$query = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // returns data to function
curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
$data = curl_exec($ch);
// if you want to display error messages, do it before curl_close()
//echo curl_errno($ch); // if you want to display error number
//echo curl_error($ch); // if you want to display error message
curl_close($ch);
return $data;
}
echo get_content('http://localname.local/directory/page.php');
我认为你需要增加CURLOPT_TIMEOUT时间
真正的问题
test.php
使用session_start()
test.php
在page.php
上启动 page.php
使用session_start()
<<< 冻结
POST request
我们不能同时使用 2 个会话 PHP。会话正在锁定并创建无限循环。
谢谢大家帮我找到解决办法。