ob_flush()不工作?

时间:2016-09-29 17:56:19

标签: php api shopify flush

我读了其他似乎有相同问题的问题,但没有一个回复对我有用,所以我会再试一次。

我正在使用Shopify的API。我需要更新每个产品的图像属性。我有超过1600种产品。我需要为每个产品进行7次API调用。因为Shopify每半秒只允许1次API调用,所以我已经加入了睡眠。问题是我明显地通过将PHP减慢到800秒来达到超时。

我尝试添加ob_flush尝试没有超时,但它没有工作,也没有输出任何东西。如果有帮助,我会使用Safari。这是我的代码:

<?php
if (ob_get_level() == 0) ob_start();

$count = json_decode(file_get_contents("https://xxxx:xxxx@mystore.myshopify.com/admin/products/count.json"));
sleep(500000);
$count = $count -> count;
$modulo = $count % 50;
$loop = ($count-$modulo)/50;
$a=0;
for ($x = 1; $x < $loop; $x++){
$products1 = json_decode(file_get_contents("https://xxxx:xxxx@mystore.myshopify.com/admin/products.json?page=$x"));
usleep(500000);
for ($y = 0; $y < 50; $y++) {
    echo $a++;
    echo ") ";
    echo $products1 -> products[$y] -> id;
    $title = $products1 -> products[$y] -> title;
    $id = $products1 -> products[$y] -> id;
    if (strpos($title, 'xxxx') !== false) {
        echo ": skipped (doesn't have extra pics)";
        continue;
    }else{
        $images = json_decode(file_get_contents("https://xxxx:xxxx@mystore.myshopify.com/admin/products/".$ID."/images.json"));
        $onlyImage = $images -> images[0] -> id;
        $url = "https://xxxx:xxxx@mystore.myshopify.com/admin/products/".$id.".json";
        $product = array(
            "product" => array(
            "id" => $ID,
            "images" => array(array($onlyImage)),
            ),
        );
        $data_json = json_encode($product);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true); 
        $response  = curl_exec($ch);
        curl_close($ch);
        if (strpos($response, '200') !== false) {
            echo ': Images Deleted ('.$title.')';
        }else{
            echo ': Failed to Delete Images ('.$title.')';
        }
        sleep(1);
        $variants = json_decode(file_get_contents("https://xxxx:xxxx@mystore.myshopify.com/admin/products/".$id."/variants.json"));
        $count = json_decode(file_get_contents("https://xxxx:xxxx@mystore.myshopify.com/admin/products/".$id."/variants/count.json"));
        $variants_count = $count -> count;
        $variants_list = array();
        for ($z = 0; $z < $variants_count; $z++) {
            $variants_list[] = $variants -> variants[$y] -> id;
        }
        $url = "https://xxxx:xxxx@mystore.myshopify.com/admin/products/".$id."/images/".$onlyImage.".json";
        $image = array(
            "image" => array(
            "id" => $onlyImage,
            "variant_ids" => $variants_list,
            ),
        );
        $data_json = json_encode($image);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true); 
        $response  = curl_exec($ch);
        curl_close($ch);
        if (strpos($response, '200') !== false) {
            echo ', Variant Images Updates ('.$title.')';
        }else{
            echo ', Failed to Update Variant Images ('.$title.')';
        }
        usleep(1500000);
    }
    echo "<br>";    
    ob_flush();
    flush();
}
}
ob_end_flush();
?>

0 个答案:

没有答案