douzhicui2209 2017-03-10 18:19
浏览 68
已采纳

调用URL并在没有响应时继续

I have a php script (plugin) which is importing some products in my site. To import the products I'm forced to use two links who are executing this import.

The first URL exits to trigger the Import. This URL unlocks the second URL to import the products. If the import is finished, the second URL, who exits to execute the import, the URL displays a code (200).

My problem now is, how can I check e.g. every 30 seconds if the second URL is returning 200? - Because this is the only way to recognize if a import is finished.

I already tried fopen(..) but the problem there is that my script gets stopped because fopen doesn't gets a return.

So what I need is a function or a code which checks e.g. if the second URL is returning something after 10s and if not the code sleeps for 30 seconds and checks it again. And only if I get returned 200, I can exit this loop.

As I said, I already tried fopen(..) but this function stops my script. And my second way to try this is curl. But the problem there is, I just do not check if I get something returned... Here my curl code:

    $ch = curl_init("https://myurl.de/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
  • 写回答

2条回答 默认 最新

  • douxin8610 2017-03-10 18:30
    关注

    You will not be able to do this using php itself -as long as you don't want to execute a process which is blocks the I/O-.

    you may work around this with a little help from javascript's setInterval or setTimeout.

    <script>
        var timer = setInterval(function () {
            // executing some ajax to call php url
            // which is check for the response status
            $.ajax({
            ....
            success: function(data) {
                // check if the response is finished
                clearInterval(timer);
    
            }
            ....
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改