使用CURL循环的问题请求超时

时间:2018-09-26 16:36:44

标签: php loops web-scraping http-headers request-timed-out

我有这样的脚本:

function getNewProductToEcommerce($merk, $shopID){
    // assuming I have called a database connection
    -----------------------------------------------
    $conn = intialitationDB();
    -----------------------------------------------
    $sql = "select * from tablebarang where merk = '".$merk."'";
    $result = mysqli_query($conn, $sql);
    $addNewProducts = array();
    foreach($result as $products){
        $tempSKU = searchEcommerceProducts($products['sku'], $shopID);

        if($tempSKU === false){
            $newProducts['sku'] = $products['sku'];
            $newProducts['catID'] = getCatIdEcommerece($merk, $products['sku']);
            $images = getPhotosSomeWebsite($merk, $products['sku']);
            for($i=1; $i <= 5; $i++){
                if(isset($images[$i-1])) $img = 'https://somewebsite.com/file/'.$images[$i-1];
                else $img = '';

                $newProducts['images_'.$i] = $img;
            }
            $addNewProducts['sku'][] = $products['sku'];
            $addNewProducts['item'][] = $newProducts;
        }
        // Markup
        // In here i should respone to user one by one, but i cannot because 
        i just need respone just some variable
    }
    return $addNewProducts;
    mysqli_close($conn);
}

function searchEcommerceProducts($sku, $shopID){
    $q = str_replace(" ","%2B", $sku);
    $url = "https://somewebsite.com/search/product?shop_id=$shopID&ob=11&rows=80&start=0&device=desktop&source=shop_product&q=$q";
    $html = file_get_contents($url);
    $html = json_decode($html, true);
    if($html["header"]["total_data"] >= 1) return true;
    else return false;
}

function getCatIdEcommerece($merk, $sku){
    $merk = str_replace(" ","%2B",$merk);
    $sku = str_replace(" ","%2B",$sku);
    $search = $merk . "%2B" . $sku;
    $url = "https://somesite.com/search/product/v3?scheme=https&device=desktop&catalog_rows=0&rows=1&source=search&ob=23&st=product&q=".$search."&sc=0&user_id=470833&rows=60&unique_id=35e66d287a5d4cefb1a674678be311f4";
    $html = file_get_contents($url);
    $html = json_decode($html, true);

    if (isset($html['data']['products'][0]['url'])){
        $url = $html['data']['products'][0]['url'];
        $cat_id = after_last ("catid%3D", $url);
    }else $cat_id = '';

    return $cat_id;
}

function getPhotosSomeWebsite($merk, $sku){
    $search = str_replace(' ','%20',$merk.' '.$sku);
    // assuming I have called a function name theCURL
    -----------------------------------------------
    $getFoto = theCURL("https://somesite.com/search_items/?by=relevancy&keyword=$search&limit=1&match_id=16775174&newest=0&order=desc&page_type=shop&__classic__=1",'GET','');
    -----------------------------------------------

    $getFoto = json_decode($getFoto, true);
    $items = $getFoto['items'];
    if(!empty($items)){
        $idProduct = $items[0]["itemid"];
        $getFoto = theCURL("https://somesite.com/item/get?itemid=$idProduct&shopid=16775174&__classic__=1",'GET','');
        $getFoto = json_decode($getFoto, true);
        return $getFoto['item']['images'];
    } else return null;
}

说明:

第一: 我需要从我的刮刮网站获取SKU,CategoryID,ImagesURL的变量,而我没有在商店中输入商品,因此我不需要检查双重商品/重复商品。

第二: 我从其他类调用函数getNewProductToEcommerce(),只是在数组中返回变量SKU,CategoryID,ImagesURL。我需要将产品项保存到xlsx。

问题:

在循环中,我只想得到一些产品,其中我没有在我的商店中输入商品,但是循环使用1000 sku产品会花费很长时间,并且返回请求超时(仅适用于100 sku产品检查)。

我一直在循环中使用echo来欺骗脚本foreach,对于此1000 sku产品来说,它工作正常,但是当我传递函数时,它并不能返回错误“标头已发送标头”。

我的问题

我如何获得许多产品的返回功能而又不会出现错误“标题已发送标题”或请求超时的问题?

如果有人可以在这个问题上帮助我,我非常感谢。

1 个答案:

答案 0 :(得分:0)

出现

已发送标题”错误是因为PHP在发送标题之前打印了一些错误或警告。查找发生哪种错误。根据您的情况,很可能是php执行超时问题,因此请根据您的需要尝试增加php配置文件中的执行时间限制:

在php.ini文件中:

max_execution_time=500

也用于内存限制:

memory_limit = 256M

如果您没有足够的特权执行操作,则可以尝试将其作为cron作业批量执行。