2017-07-30 49 views
0

当我发送帖子请求时,有一些问题。 我用这个脚本执行页面http://localname.local/test,页面http://localname.local/directory/page.php得到一个json数据。curl或file_get_contents不起作用

$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个secondes,剧本得到的消息:

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)可以”访问或发送请求到自己的页面。

我得到了PHP 7.1.7,Apache 2.4.23在wam 3.0.9上,选项allow_url_fopen打开。

编辑: 为卷曲

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

+0

“test.php”通常(甚至有时)是否需要超过5秒才能运行? – ceejayoz

+0

不,它运行速度很快。 WAMP配置为30秒,但在控制服务器之前我必须等待太久。 :/ – Rodrigue

+0

这个问题只是一个访问错误,我认为。 WAMP有可能无法访问自己的域名吗?我在本地主机上运行这个文件 – Rodrigue

回答

0

真正的问题

  1. test.php使用session_start()
  2. test.php推出的page.php
  3. page.php一个POST request使用session_start()< < <冻结

我们不能用2届PHP在同一时间。会话锁定并创建无限循环。

谢谢大家帮我找到解决方案。

1

对于file_get_contents()函数打开网址,您必须启用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'); 
+0

Get'Warning:curl_errno():提供的资源不是位于D行的D:\ ... \ html \ test.php中的有效cURL句柄资源“ – Rodrigue

+1

这个错误是因为你使用' 'curl_close()'后面的curl_errno()'你需要在'curl_close($ curl)'之前保留任何错误处理。 – Yolo

+0

哦,是的,对不起。谢谢,但现在的错误是'致命的错误:未捕获异常:操作超时10000毫秒后收到0字节D:\ ... \ html \ test.php在X线上但我认为问题是拒绝沟通(他自己的域名)是可能的吗? – Rodrigue

0

我认为你需要增加CURLOPT_TIMEOUT时间

+0

不,很抱歉,无论是30秒后停止的PHP脚本,还是CURL查询以CURLOPT_TIMEOUT时间停止。该脚本在localhost中很快 – Rodrigue