无法连接主机:80:cURL php超时

时间:2019-07-02 15:35:45

标签: php curl laravel-5

我有两个申请。一个是PHP,另一个是Laravel应用,其中两者都位于不同的服务器上。我想将数据从PHP应用程序发送到Laravel应用程序,而我正在使用cURL。但这会引发“连接失败:超时”错误。

这就是我使用cURL通过POST发送json数据的方式:

$url ='http://host_name/project/public/index.php/login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$userdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);

if (curl_error($ch)) {
    $error_msg = curl_error($ch);
}

echo $error_msg."<br>";

print_r(curl_getinfo());

我得到的输出是:

Failed to connect to host_name port 80: Timed out

   [url] => http://host_name/project/public/index.php/login
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 20.234
    [namelookup_time] => 0.015
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 
    [certinfo] => Array
        (
        )

    [primary_port] => 0
    [local_ip] => 
    [local_port] => 0
)

1 个答案:

答案 0 :(得分:-1)

这可能是防火墙问题。 默认情况下,端口80可能被阻止。也许将您的请求更改为https 它应该解决问题。 或者,如果失败,请尝试将其放入您的curl请求中 curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false)

如果该操作仍然失败,则需要查看防火墙。