curl页面加载失败并显示错误:使用set_time_limit(0)时请求超时

时间:2016-03-21 14:00:48

标签: php

运行一些cURL语句时出现错误:Page load failed with error: The request timed out.

我用谷歌搜索它,解决方案应该使用set_time_limit(0)(我不是在无限循环中。)

我添加了此代码,但错误仍然存​​在。为什么呢?

我也注意到,我的所有echo语句都没有打印出来。为什么会这样?

CODE:

<?php
require_once 'login.php';
//connect to database
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error){
    die("error connecting");
}else{
    echo "connected to database places<br>";
}
set_time_limit(0);

$url = "https://order.pizzahut.com/site/ajax/find_nearby";
$ch = curl_init($url);
$useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';  // Setting useragent of a popular browser

$header = array("Accept: application/json, text/plain, */*",
        "Cookie: failover_checked=1; PHPpoolSSL=!4maklee1+k5+tBLWWPR8rI/3M2NcIXVsgM+5XMIVRK/fJJleEnP4hyVoopcRquvCAs/7CbLq/Vfd0cc=; TS011d6839=01166f2bd0e693828ed3fb37eef086402c5626aca6908460b9722b154cb51f244b1dd882278db1bb1f9ac97a3f2d3617b12e7c7b5c; www-origin=yeti-HR-HARYANA-; www-origin-reset=1; exp_last_visit=1143192473; optimizelyEndUserId=oeu1458555784471r0.9519811202417545; _gat_UA-34361514-1=1; QOSESSID=6ohua5ugd55mjupf0p6k32uia6; _dc_gtm_UA-34361514-1=1; exp_last_activity=1458560242; exp_tracker=a%3A5%3A%7Bi%3A0%3Bs%3A12%3A%22site%2Flocator%22%3Bi%3A1%3Bs%3A4%3A%22home%22%3Bi%3A2%3Bs%3A5%3A%22index%22%3Bi%3A3%3Bs%3A23%3A%22site%2Fajax%2Fclear_session%22%3Bi%3A4%3Bs%3A38%3A%22site%2Fajax%2Fcoupons%2FremoveNationalCoupon%22%3B%7D; TS01bded03=01166f2bd034b629d99e25089af83586e4bf2347f7b1f70b91972e835cfd37e486120c510cfabe2bd9856390632a8276d8250ae05869b06cc1d6a7bc77ee826c8b208bc7361686c0b1cbd41a8530948d8fa923b0b3; TS0118ed69=01166f2bd0c9a8c11fd75a87aad59a90e93a811cbea110792544f73601f99f85ef0de952c0bb1c1706650ace601420df60560355a33fa2ecea9f8f89e1fdd807c0f5717ab8fe2b84c6305f1add271e312787310fa30c484c0174c988835bcea2ced9db5fcf; optimizelySegments=%7B%22209642986%22%3A%22referral%22%2C%22209669694%22%3A%22gc%22%2C%22209692476%22%3A%22false%22%2C%22209740042%22%3A%22none%22%2C%22307825635%22%3A%22true%22%7D; optimizelyBuckets=%7B%7D; _ga=GA1.2.700288496.1458555787; optimizelyPendingLogEvents=%5B%5D",
);
$options = [
        CURLOPT_USERAGENT => $useragent,
        CURLOPT_COOKIEJAR => "cookie.txt",
        CURLOPT_COOKIE => "cookie.txt", 
        CURLOPT_REFERER =>"https://www.google.com",
        CURLOPT_POST=>true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTPHEADER =>$header,   
];
curl_setopt_array($ch, $options);

for ($zip="01001";$zip<"99950";$zip+=4){
    loadPageAndSaveData($zip,$ch);
}

function loadPageAndSaveData($zip,$ch){
    global $conn;
    $zipStr;
    if (strlen($zip)<5){
        $zipStr = "0"."$zip";
    }else{
        $zipStr = $zip;

    }
    echo "$zipStr ";
    echo date('h:i:s A')."<br>";



    $postData = [
            'occasion'       => "C",
            'near'          => "$zipStr",
            'zip'        => "$zipStr",
            'address'       => "",
            'dine_in'        => false,
            'store_limit'   =>20,
            'locator'   =>true

    ];

    $payload = json_encode($postData);
    # Convert the post data array to URL encoded string
    $postDataStr = http_build_query($postData);


    $options[CURLOPT_POSTFIELDS] = $postDataStr;
    curl_setopt_array($ch, $options);

    $html = curl_exec($ch);

    // SAVE TO MYSQL DB
    $json = json_decode($html,true);
    $count = count($json['response']);
    echo "$count<br>";

    if ($count>0){
            for ($x = 0; $x < $count; $x++) {
                $address = mysql_real_escape_string($json['response'][$x]['address']);
                $city = mysql_real_escape_string($json['response'][$x]['city']);
                $state = mysql_real_escape_string($json['response'][$x]['state']);
                $zip = mysql_real_escape_string($json['response'][$x]['zip']);
                $storeNumber = mysql_real_escape_string($json['response'][$x]['StoreNumber']);

                $query = "INSERT INTO pizzahut VALUES('{$address}','{$city}','{$state}','{$zip}','{$storeNumber}','{$zipStr}')";
                $result = $conn->query($query);
                if(!$result){
                    die("failed to add data $conn->error ");
                }else{

                }
            }   
    }
}

0 个答案:

没有答案