duan205571 2019-03-15 17:44
浏览 286

如何在php循环中执行curl?

I have used postman to execute curl request and get response from the api. Everything is working perfectly. But as soon as I try to execute same curl within a php while loop. It doesn't works at all.

Searched over the internet but did not found any useful information. I would like to know if there is any other way, other than using curl. I also tried sleep(3) function from php which sleeps loop for 3 seconds but it didn't helped.

Bottom line curl is not working in php loop whereas it works independently.

Below is my code for reference

        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                       
                }
            }
        }
    }

I tried putting curl init and other setopt before the while loop, but it is not working either.

    $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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog