如何在php循环中执行curl?

时间:2019-03-15 17:44:38

标签: php api loops curl

我已经使用邮递员执行curl请求并从api获取响应。一切工作正常。但是,一旦我尝试在php while循环内执行相同的curl。它根本不起作用。

通过互联网搜索,但未找到任何有用的信息。我想知道是否除了使用curl以外还有其他方法。我还尝试了php中的 sleep(3)函数,该函数使循环睡眠3秒钟,但没有帮助。

底线 curl不能在php循环中工作,而可以独立工作。

以下是我的参考代码

        while (($company = fgets($handle)) !== false) {
        // process the line read.
        $company = trim($company);
        if(!empty($company)) {
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.domain.com/services/v3/api/entities?search-term=$company&page-size=10000",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_POSTFIELDS => "",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Bearer 78654356gvgfdr43wsdxcfg",
                    "cache-control: no-cache"
                ),
            ));
            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);
            $response_array = json_decode($response, TRUE);

            $items = $response_array['items'];
            if (!empty($items)) {
                foreach ($items AS $item) {
                    $tt_id = $item['tt_id'];
                    $tt_Name = $item['tt_Name'];
                    $tt_TypeID = $item['tt_TypeID']['id'];                      
                    //save to db                       
                }
            }
        }
    }

我尝试将curl init和其他setopt放在while循环之前,但是它也不起作用。

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($curl, CURLOPT_POSTFIELDS, "");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer 78654356gvgfdr43wsdxcfg",
        "cache-control: no-cache"
    ));
    while (($company = fgets($handle)) !== false) {
        // process the line read.
        $company = trim($company);
        if(!empty($company)) {
            curl_setopt($curl, CURLOPT_URL, https://api.domain.com/services/v3/api/entities?search-term=$company&page-size=10000);               
            $response = curl_exec($curl);
            $err = curl_error($curl);
            $response_array = json_decode($response, TRUE);             

            $items = $response_array['items'];
            if (!empty($items)) {
                foreach ($items AS $item) {
                    $tt_id = $item['tt_id'];
                    $tt_Name = $item['tt_Name'];
                    $tt_TypeID = $item['tt_TypeID']['id'];                      
                    //save to db
                }
            }
        }
    }
    curl_close($curl);

0 个答案:

没有答案