doudi4137 2017-10-24 09:54
浏览 626
已采纳

如何通过cURL获取数据并等待结果?

I have a list of IDs. For test this is about 20 items.

Now I want to make a loop and get extra data from another server. On this server I prepared a script - when you send it an item ID it will send back more details in JSON format.

while($row = mysql_fetch_array($result)){   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://test.mysite.abc/call/itemdetail/id/".$row['id_item']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $ret = curl_exec ($ch);
    curl_close ($ch);

    $result = json_decode($ret, true);

    print_r($result);
    vecho "<br>";
}

When I go on mysite.abc I see result when I give an item id. But when I run script sometimes I have 1 result or 3 results and the message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:\wamp64\www\testfile.php on line 65

How should I do this?

  • 写回答

1条回答 默认 最新

  • dsf45346 2017-10-24 13:23
    关注

    Check mysql_query returned a valid resource type.

    $result = mysql_query('SELECT column1, column2 FROM table WHERE 1=1'); // Change the SQL query with your
    
    if ($result) {
        while($row = mysql_fetch_array($result)){   
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://test.mysite.abc/call/itemdetail/id/".$row['id_item']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $ret = curl_exec ($ch);
            curl_close ($ch);
    
            $result = json_decode($ret, true);
    
            print_r($result);
            echo "<br>";
        }
    }
    

    Best Practices:, Avoid mysql_* extension. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, use the MySQLi or PDO_MySQL extension.

    Consider using multi-curl for your case. Look this answer for more details.

    Cheerse!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)