10

我一直在关注如何使用curl_multi. http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/

我不知道我做错了什么,但curl_multi_getcontent返回 null。假设返回 JSON。我知道这不是 mysql 调用,因为我使用了 while 循环和 standard curl_exec,但是页面加载时间太长。(为了安全起见,我更改了一些 setopt 细节)

相关的 PHP 代码片段。最后我确实关闭了while循环。

$i = 0;
$ch = array();
$mh = curl_multi_init();
while($row = $result->fetch_object()){
   $ch[$i] = curl_init();
   curl_setopt($ch[$i], CURLOPT_CAINFO, 'cacert.pem');
   curl_setopt($ch[$i], CURLOPT_USERPWD, "$username:$password");
   curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); 
   curl_setopt($ch[$i], CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
   curl_multi_add_handle($mh, $ch[$i]);
   $i++;
}
$running = 0;
do {
    curl_multi_exec($mh, $running);
} while ($running > 0);
$result->data_seek(0);
$i = 0;
while ($row = $result->fetch_object()) {
    $data = curl_multi_getcontent($ch[$i]);
    $json_data = json_decode($data);
    var_dump($json_data);

编辑

这是当前有效的代码,但会导致页面加载速度过慢

$ch = curl_init();
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
while($row = $result->fetch_object()){
   curl_setopt($ch, CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
   $data = curl_exec($ch);
   $json_data = json_decode($data);
   var_dump($json_data);
}
4

5 回答 5

2

我在想:

$i = 0;
while ($row = $result->fetch_object()) {
    $data = curl_multi_getcontent($ch[$i]);
    $json_data = json_decode($data);
    var_dump($json_data);

你忘记增加 $i 了吗?如果是这样,您已经获取了 $ch[0] 的内容,然后您再次调用 curl_multi_getcontent。

另外,我写了一篇博客文章,涵盖了使用 PHP 的 cURL 扩展的并发请求,它包含了一个用于 curl 多请求的通用函数。您可以通过以下方式调用此函数:

$responses = multi([
    $requests = [
        ['url' => 'https://example.com/search/username1/'],
        ['url' => 'https://example.com/search/username2/'],
        ['url' => 'https://example.com/search/username3/']
    ]
    $opts = [
        CURLOPT_CAINFO => 'cacert.pem',
        CURLOPT_USERPWD => "username:password"
    ]
]);

然后,您循环浏览响应数组:

foreach ($responses as $response) {
    if ($response['error']) {
        // handle error
        continue;
    }
    // check for empty response
    if ($response['data'] === null) {
        // examine $response['info']
        continue;
    }
    // handle data
    $data = json_decode($response['data']);
    // do something
}

使用此函数,您可以使用以下调用对访问 https 站点进行简单测试:

multi(
    $requests = [
        'google' => ['url' => 'https://www.google.com'],
        'linkedin' => ['url'=> 'https://www.linkedin.com/']
    ],
    $opts = [
        CURLOPT_CAINFO => '/path/to/your/cacert.pem',
        CURLOPT_SSL_VERIFYPEER => true
    ]
);
于 2013-09-23T08:16:10.140 回答
1

我看到您的执行循环与PHP 文档中建议的执行循环不同:

do {
  $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

注意在while函数中返回的是比较的,而不是第二个参数。

编辑:感谢亚当的评论,我测试了这两种语法,发现它们是平等的和异步的。这是一个将内容放入变量的异步多请求的工作示例:

<?php
$ch = array();
$mh = curl_multi_init();
$total = 100;

echo 'Start: ' . microtime(true) . "\n";

for ($i = 0; $i < $total; $i++) {
    $ch[$i] = curl_init();
    curl_setopt($ch[$i], CURLOPT_URL, 'http://localhost/sleep.php?t=' . $i);
    curl_setopt($ch[$i], CURLOPT_HEADER, 0);
    curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);

    curl_multi_add_handle($mh, $ch[$i]);
}

$active = null;
do {
    $mrc = curl_multi_exec($mh, $active);
    usleep(100); // Maybe needed to limit CPU load (See P.S.)
} while ($active);

foreach ($ch AS $i => $c) {
    $r = curl_multi_getcontent($c);
    var_dump($r);
    curl_multi_remove_handle($mh, $c);
}

curl_multi_close($mh);

echo 'End: ' . microtime(true) . "\n";

并测试文件 sleep.php:

<?php
$start = microtime(true);

sleep( rand(3, 5) );

$end = microtime(true);

echo $_GET['t'], ': ', $start, ' - ', $end, ' - ', ($end - $start);
echo "\n";

PSusleep在循环中使用的最初想法是暂停它一点,从而减少 cUrl 等待响应时的操作数量。一开始它似乎是这样工作的。但最后top的测试显示 CPU 负载差异很小(17%usleep与 20% 没有它)。所以,不知道要不要用。也许在真实服务器上的测试会显示另一个结果。

编辑 2:我已经通过向受密码保护的 HTTPS 页面发出请求来测试我的代码(CURLOPT_CAINFO并且CURLOPT_USERPWD等于问题中的那些)。它按预期工作。您的 PHP 或 cURL 版本中可能存在错误。我的版本是“PHP 版本 5.3.10-1ubuntu3.8”和 7.22.0。他们没有问题。

于 2013-09-23T15:00:13.400 回答
0

$running = null;代替$running = 0;. _

根据链接:

  1. 多个curl-requests-with-php

  2. http://www.php.net/manual/en/function.curl-multi-exec.php

在这两种情况下,变量都被定义为 NULL,这是因为

curl_multi_exec ( resource $mh , int &$still_running )

第二个参数是对变量的引用。

此外,您可能会发现这很有用:php single curl works but multi curl doesn't work

于 2013-09-22T10:10:03.607 回答
0

您是否将 CURLOPT_SSL_VERIFYPEER 设置为 true?

于 2013-09-23T14:55:50.037 回答
-1

curl_multi_exec执行多线程 HTTP 请求,并且请求可能不是按照您将它们添加到 multihandler 的顺序完成的$mh。要获得已完成请求的响应,您应该使用curl_multi_info_read函数。您可以在 php.net http://php.net/manual/ru/function.curl-multi-info-read.php上阅读更多相关信息

于 2013-09-18T10:51:58.260 回答