【问题标题】:How to send multiple URLs simultaneously with PHP?如何使用 PHP 同时发送多个 URL?
【发布时间】:2025-11-25 13:45:01
【问题描述】:

我想同时发送不同的网址。这是我使用的代码。你能告诉我这里的错误吗?

<?php
    $data = R::find('savedata','list_name = ? order by id desc',array($i));

    foreach ( $data as $list):
        $mh = curl_multi_init(); //set up a cURL multiple execution handle
        $ch = curl_init("https://example.com/save_data.php?NUM=$list->id&MSG=$message"); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);           
        curl_multi_add_handle($mh, $ch); 
    endforeach; 

    curl_multi_exec($mh,);                          
    curl_multi_close($mh)
?>

【问题讨论】:

  • 你得到什么错误?
  • 首先 - $data 是什么样的?其次,curl_multi_exec 中有一个浮动逗号。第三,你确定你的 URL 是正确的并且类变量扩展了吗?第四,$message从何而来?第五,您为每次迭代初始化 curl_multi_init - 这应该只做一次。
  • 这里我想同时发送几个网址。我尝试了几种方法,但都没有成功。@h2ooooooo 网址是正确的。
  • @Theolodies- 我没有得到任何结果。

标签: php curl


【解决方案1】:

谢谢大家。我自己更正了这个问题。我正在与你分享正确的代码,

$mobile = R::find('add_numbers', 'list_name = ? order by id desc', array($i));

foreach ($mobile as $list):
    try{
  $url = "https://example.com/save_data.php?NUM=$list->id&MSG=****";
    $ch = curl_init($url);

   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE
   curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   $results = curl_exec($ch);
     $i[]=$results;
    }

【讨论】: