Php 卷曲或文件\u获取\u内容无效

Php 卷曲或文件\u获取\u内容无效,php,curl,wamp,wampserver,file-get-contents,Php,Curl,Wamp,Wampserver,File Get Contents,我在发送POST请求时遇到一些问题。 我执行页面http://localname.local/test使用此脚本和页面http://localname.local/directory/page.php获取json数据 $url = "http://localname.local/directory/page.php"; $post = [ "key1" => "hello", "key2" =

我在发送POST请求时遇到一些问题。 我执行页面
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秒钟后,脚本会收到以下信息:

警告:文件\获取\内容():无法打开流:HTTP请求失败!在D中:\\第X行的html\test.php

php可以工作,我可以用浏览器作为客户端发送POST请求,但php(或wamp)不能访问或发送请求到自己的页面

我在wamp 3.0.9上获得了PHP7.1.7、Apache 2.4.23,并且启用了选项
allow\u url\u 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;
}
得到

致命错误:未捕获异常:操作在10000毫秒后超时,在D:\\第X行的html\test.php

异常:操作在10000毫秒后超时,接收到0字节

要使文件_get_contents()打开URL,必须在php.ini文件中启用该设置

安全方面,我建议您实现您正在使用的功能:


我认为您需要增加CULLOPT_超时时间

真正的问题

  • test.php
    使用
    session\u start()
  • test.php
    page.php

  • page.php
    使用
    session\u start()
    获取
    警告:curl\u errno():提供的资源不是D:\\html\test.php第X行
    导致该错误的原因是您在
    curl\u close()
    之后使用了
    curl\u errno()
    ,您需要在
    curl\u close($curl)
    之前保留任何错误处理。哦,是的,对不起。谢谢,但现在错误是
    致命错误:未捕获异常:操作在10000毫秒后超时,在D:\\html\test.php在第X行
    但我认为问题在于拒绝与他本人(与他自己的域名)沟通。有可能吗?这是因为你的page.php处理你的请求花费的时间太长。你需要找到那里的瓶颈。你是在绕着什么东西转吗?你在那个代码里做什么?您可以稍后使用
    curl\u setopt($ch,CURLOPT\u timeout,30)将curl设置为timeout(30秒),然后看看你是否能找回什么。但是显然,我们必须在page.php上查找原因。
    test.php
    运行通常(甚至有时)需要五秒钟以上吗?不,运行速度很快。WAMP配置为30秒,但我必须等待太长时间才能控制服务器:/我认为问题只是一个访问错误。WAMP是否可能无法访问自己的域名?我在localhostNo上运行这个,对不起,要么是PHP脚本在30秒后停止,要么是CURL查询在CURLOPT_超时时停止。在localhost中,脚本速度很快
    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');