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

如何通过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?

图片转代码服务由CSDN问答提供 功能建议

我有一个ID列表。 对于测试,这是大约20项。

现在我想做一个循环并从另一台服务器获取额外的数据。 在这台服务器上,我准备了一个脚本 - 当你发送一个项目ID时,它会以JSON格式发回更多细节。

  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($ 结果); 
 vecho“&lt; br&gt;”; 
} 
   
 
 

当我继续使用mysite.abc时,我会在给出项目ID时看到结果。 但是当我运行脚本时,我有1个结果或3个结果和消息:

警告:mysql_fetch_array()期望参数1是资源,数组 给出 第65行的C:\ wamp64 \ www \ testfile.php

我该怎么做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    点赞 打赏 评论

相关推荐 更多相似问题