gpt4 book ai didi

php - 在 While 循环中执行 PHP CURL

转载 作者:行者123 更新时间:2023-12-04 17:52:46 25 4
gpt4 key购买 nike

我有一个表,我想在维护期间在其中运行 PHP 脚本。

我有以下代码。

$sql = "SELECT `id`, `url` FROM `log_requests` WHERE `is_sent` = '0'";
$e = $conn->execute($sql);

我的 url 从数据库中获取表后的数组。

Array
(
[0] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+1
[1] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+2
[2] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+3
[3] => https://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=username&Password=password&Type=Individual&To=9999999999&Mask=Senderid&Message=Hello+World+4
)

然后我运行 while 循环

$ch = curl_init();
while($row = $e->FetchRow() ){
curl_setopt($ch, CURLOPT_URL, $row['url']);
curl_setopt($ch, CURLOPT_HEADER, 0);
$q = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '".$row['id']."'";
$conn->execute($q);
}
curl_close($ch);

问题是只有一个 URL 是使用 CURL 执行的,在我的表中只有一行在 is_sent 列中更新为 1。

为什么它在我的 while 循环中运行单个 url。我在这里犯了什么错误?

*************************** 编辑 *********************

好的,现在我已经跟随并成功运行了。我只想知道我做的对不对。

$set = array();
while ($row = $executequery2->FetchRow()) {
$set[$row['url']][] = $row;
}

$curl_handles = array();
foreach($set as $url => $rows) {
$curl_handles[$url] = curl_init();
curl_setopt($curl_handles[$url], CURLOPT_URL, $url);
foreach($rows as $row) {
$query3 = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '" . $row['id'] . "'";
$conn->Execute($query3);
}
}

$curl_multi_handle = curl_multi_init();
$i = 0;
$block = array();

foreach($curl_handles as $a_curl_handle) {
$i++;
curl_multi_add_handle($curl_multi_handle, $a_curl_handle);
$block[] = $a_curl_handle;
if (($i % 5 == 0) or ($i == count($curl_handles))) {
$running = NULL;
do {
$running_before = $running;
curl_multi_exec($curl_multi_handle, $running);
if ($running != $running_before) {
echo ("Waiting for $running sites to finish...<br />");
}
}
while ($running > 0);
foreach($block as $handle) {
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$curl_errno = curl_errno($handle);
$curl_error = curl_error($handle);
if ($curl_error) {
echo (" *** cURL error: ($curl_errno) $curl_error\n");
}
curl_multi_remove_handle($curl_multi_handle, $handle);
}
$block = array();
}
}

curl_multi_close($curl_multi_handle);

更新表格列是正确的方法吗?

最佳答案

我遇到了同样的问题并在线获得了这个解决方案。只需将 curl 代码从循环中分离出来,然后尝试。

像这样:

while($row = $e->FetchRow()) {
$result = call_curl($row['url']);
if($result === FALSE) {
die("Error");
}
else
{
$q = "UPDATE `log_requests` SET `is_sent` = '1' WHERE `id` = '".$row['id']."'";
}
}


function call_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}

希望这对你也有帮助。

关于php - 在 While 循环中执行 PHP CURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43180026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com