【问题标题】:Show default page when Curl takes too long to connect to API当 Curl 连接 API 的时间过长时显示默认页面
【发布时间】:2015-05-15 10:00:54
【问题描述】:

我目前在连接到 api 服务的网站主页上有一个脚本。有时 api 响应时间过长,如果响应时间超过三秒,我试图找出一种显示默认结果的方法。我不知道该怎么做。这是我的 curl 脚本:

class Ofertas{
public $Name;
public $HotelId;
public $Coordinates = array();
public $Longs;
public $Response = array();


public function __construct($var){

$ofertas = new SqlIt('SELECT destination_id, destination_name, bottom_link, link FROM **** ORDER BY slot ASC LIMIT '.$var,'select',array());

foreach($ofertas->Response as $did){
    $coor = new SqlIt("SELECT CenterLatitude, CenterLongitude, RegionName FROM ***** WHERE RegionID = ? LIMIT ".$var,"select",array($did->destination_id));
    array_push($this->Coordinates,array($coor->Response[0]->CenterLatitude,$coor->Response[0]->CenterLongitude,$coor->Response[0]->RegionName));
}
$i = 0;
$url = "http://apiurl";
$curl_arr = array();
$master = curl_multi_init();
$i = 0;
foreach($this->Coordinates as $co){
    $xml = '<xmlrequesthere>xml code goes here for request</xmlrequesthere>';

            $curl_arr[$i] = curl_init($url);
            curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl_arr[$i], CURLOPT_POSTFIELDS, $xml); 
            curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl_arr[$i], CURLOPT_HTTPHEADER, array('Accept: application/xml')); 
            curl_setopt($curl_arr[$i], CURLOPT_HEADER, 0);
            curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT ,1); // number of seconds to wait for initial connection
            curl_setopt(curl_arr[$i], CURLOPT_TIMEOUT, 2); //timeout for entire process.
            curl_multi_add_handle($master, $curl_arr[$i]);



        do {
            curl_multi_exec($master,$running);
        } while($running > 0);

        if (curl_errno($curl_arr[$i])) {
                //fetch default hotels from the database

        }else{

            $results = curl_multi_getcontent  ( $curl_arr[$i]  );
            $apit = simplexml_load_string($results);
            $j['destination'] = $ofertas->Response[$i]->destination_name;
            $j['button'] = $ofertas->Response[$i]->bottom_link;
            $j['link'] = $ofertas->Response[$i]->link;
            $j['hotels'] = array();
            foreach($apit->HotelList->HotelSummary as $res){
                $hh['name'] = $res->name.'';
                $hh['hid'] = $res->hotelId.'';
                $hh['city'] = $res->city.'';
                $hh['rate'] = $res->lowRate.'';
                $hh['thumb'] = $res->thumbNailUrl.'';

                array_push($j['hotels'],$hh);
            }
        }

    array_push($this->Response, $j); 
$i++; }

    }
}//end of class

我应该在某处关闭连接吗?或者我只是想像我说的那样添加一个超时。任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: php api curl timeout


    【解决方案1】:

    以下是 2 个超时标志的示例,您可以使用这些标志在进程耗时过长时使 curl 返回。

    curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT ,1); // number of seconds to wait for initial connection
    curl_setopt(curl_arr[$i], CURLOPT_TIMEOUT, 2); //timeout for entire process.
    

    您不需要在任何地方关闭连接,因为 libcurl 将简单地通过 exec() 或类似的函数为您调用“curl”进程,它将处理所有套接字 IO。

    一旦发生超时,您可以检查是否发生错误:

    if (curl_errno($curl_arr[$i])) {
        // Error Handling code here.
    }
    

    【讨论】:

    • 谢谢达米安,快速提问.. 它会回到哪里?当它超时时,我怎么能做一个 php 动作?还是我应该重定向它?这是我最困惑的地方。非常感谢!
    • 我已经修改了我的答案以包含更多细节,以便您可以在发生超时(或任何其他错误)时执行适当的错误处理。
    • 我应该在 do{ 内部还是外部添加这个?再次感谢您的帮助!
    • 嗯,这取决于你的逻辑,你没有提供足够的代码 sn-p 来显示 $i 递增的位置。您应该在每个 curl 句柄对象上单独调用它。我在这里可以看到的一个可能问题是您在 for 循环中重复调用(因此每次调用 curl_multi_exec 都会向多卷曲句柄添加一个调用。
    • 我在 curl_multi_exec 之后添加它。我将如何纠正这一点?你正式成为我的救星!!
    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多