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



我有这样的脚本:

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、类别 ID、图像 URL 的变量,我没有在我的商店中输入商品,所以我不需要检查双重产品项目/重复产品。

第二: 我从其他类调用函数getNewProductToEcommerce(),只返回数组中的变量SKU,类别ID,图像URL。我需要将产品项目保存到 xlsx。

问题 :

在循环中,我只想获得一些我没有在我的商店中输入商品的产品,但是循环使用 1000 SKU 产品需要很长时间并返回请求超时(只是为 100 SKU 产品检查工作(。

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

我的问题

我如何获得许多产品的返回功能,而不会收到错误"标头已由标头发送"或在我的问题中请求超时?

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

">

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

在 php.ini 文件中:

max_execution_time=500

同样对于内存限制:

memory_limit = 256M

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

最新更新