dongsetan3216 2016-11-25 12:17
浏览 277
已采纳

在循环中使用cURL - 问题太慢了

I have a big database table, I fetch column to extract phone number to put it in curl POST, so how to increase time? Because I execute curl in while loop for every row found, thanks for help.

  • 写回答

2条回答 默认 最新

  • dongtong0796 2016-11-27 16:20
    关注

    When using CURL you are dependent on the time it takes to communicate with the remote server. There is no way to speed a single CURL request up except making sure your server is hosted on a good service provider which is able to load the remote website quickly.

    You can speed your script up by running CURL requests in parallel. To do so, move the CURL function to a separate PHP file, which receives the phone number as parameter through the command line arguments ($argv). The script should also include the code for updating the database with the CURL results.

    Then, use exec() to call the script. For example, if the script is checkphone.php, call it like so:

    exec('php -f /var/www/include/checkphone.php '.$phoneNumber.' > /dev/null 2>&1 &');
    

    Note that the full directory is provided. Also, use:

    /dev/null 2>&1 &

    To let the system know not to wait for the script to finish running. It will execute checkphone.php then continue to iterate through the loop.

    Words of caution: if you have hundreds of phone numbers, this will open hundreds of processes and would most likely crash your server. Build controls in your master script to ensure opening only a few concurrent processes at a time. This can be done by querying the database to see if the phone numbers you've sent as arguments have been updated with the additional information by the CURL script. Furthermore, make sure you don't send too many requests to the remote server too fast, otherwise you will end up DDoSing the server, which is a no-no. If you have several remote sites that can provide you with the information that you want, consider iterating between them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧