【问题标题】:cURL geto no response no error online servercURL geto no response no error online server
【发布时间】:2016-08-31 07:53:10
【问题描述】:

希望任何人都可以帮助我解决这个问题。我尝试使用 cURL 调用外部站点,但没有收到错误或响应。然而它在本地服务器上工作正常,但在生产服务器上不起作用。我已经使用 file_get_contents() 开始了通话,但在线也失败了。我与主机交谈,他们提到问题出在代码中。这是我的代码,谁能帮忙!?`

function send_with_curl($url, $data) {
   $data_string;
       //url-ify the data for the POST
    foreach ($data as $key => $value) { $data_string .= $key . '=' .  $value . '&';
}
rtrim($data_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);
if (!is_string($result) || !strlen($result)) {      
    $result = "Failed to connect.";
}
//close connection
curl_close($ch);
return $result;
} 

我还有另一个使用 file_get_contents() 的函数,无论我使用哪个函数,它们都可以在本地工作,但在没有错误的情况下在线失败 我花了 4 多个小时试图与托管人员一起修复它,直到他们最终说错误在代码中他们不熟悉这些代码:(

function send_with_file($url, $context, $sender) {
global $database;
if (!$result = file_get_contents($url, false, $context)) {
    echo "Message sending failed, please check your internet.";
    exit ;
} else {
    //--UPDATING THE DATABASE------
    $sql_update = "SELECT * FROM users WHERE users_type='2'";
    $query_update = $database -> query($sql_update);
    while ($update = $database -> fetch_array($query_update)) {
        $update_user = $update['users_id'];
        if ($update_user == $sender) {

            if ($result === FALSE) {/* Handle error */
            }
        } else {

        }

    }
}
return $result;
}

【问题讨论】:

    标签: php curl hosting localserver


    【解决方案1】:

    $resultfalse 进行比较,然后检查curl_error()

    类似...

    $result = curl_exec($ch);
    if($result === false) {
        echo "Error in cURL : " . curl_error($ch);
    }
    

    【讨论】:

    • 它没有给出任何响应! :'(
    • 您确定该 URL 确实返回了任何内容吗?
    • 很确定,因为在我的本地服务器上,我得到了来自 URL 的响应!但在生产服务器上,我什么也没得到,而且它无法发出请求
    • 介意分享上述网址吗?
    • 197.243.19.159:8080/staronly/rapid.htm 即 url 不传递任何参数将返回错误。但是我什至没有从托管服务器得到这个错误,但我在本地得到了它!
    【解决方案2】:

    代码没有问题,我只是跟踪了我试图连接的 url,并意识到与托管服务器通信需要很长时间。

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2021-10-13
      • 1970-01-01
      相关资源
      最近更新 更多