PHP curl_multi_getcontent返回null [英] PHP curl_multi_getcontent returns null

查看:878
本文介绍了PHP curl_multi_getcontent返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关于如何使用 curl_multi 这个教程。 http://arguments.callee.info/2010/ 02/21 / multiple-curl-requests-with-php /



我不知道我在做什么,但是 curl_multi_getcontent 返回null。假设要返回JSON。我知道这不是mysql调用,因为我使用一个while循环和标准 curl_exec ,但页面加载太长。 (我改变了一些安全的setopt细节)



相关的PHP代码片段。

  $ 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);

EDIT



  $ 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);
}


解决方案

/ p>

  $ 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。



此外,我写了一篇博文=http://adamjonrichardson.com/2013/09/23/making-concurrent-curl-requests-using-phps-curl_multi-functions/ =nofollow>使用PHP的cURL扩展的并发请求,并且它包含curl多请求的一般函数。您可以按以下方式调用此函数:

  $ responses = multi([
$ requests = [
['url'=>'https://mysite.com/search/username1/'],
['url'=>'https://mysite.com/search/username2/' ],
['url'=>'https://mysite.com/search/username3/']
]
$ opts = [
CURLOPT_CAINFO => cacert.pem',
CURLOPT_USERPWD =>username:password
]
]);然后,你循环通过响应数组:








$ b

  foreach($ responses as $ response){
if($ response ['error'] {
// handle error
continue;
}
//检查空响应
if($ response ['data'] === null){
//检查$ response ['info']
continue;
}
//处理数据
$ data = json_decode($ response ['data']);
//做某事
}

使用此功能,您可以使用以下调用进行访问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
]
);


I have been following this tutorial on how to use curl_multi. http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/

I can't tell what I am doing wrong, but curl_multi_getcontent is returning null. It is suppose to return JSON. I know it is not the mysql call as I had it working with a while loop and standard curl_exec, but The page was taking too long to load. (I've changed some of the setopt details for security)

Relevant PHP Code snippet. I do close the while loop in the end.

$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);

EDIT

Here is the code that currently works, but causes the page to load too slowly

$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);
}

解决方案

I'm wondering:

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

Are you forgetting to increment $i? If so, you already grabbed the content for $ch[0], and then you call curl_multi_getcontent again.

Also, I've written a blog post covering concurrent requests with PHP's cURL extension, and it contains a general function for curl multi requests. You could call this function in the following manner:

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

Then, you cycle through the responses array:

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
}

Using this function, you could make a simple test of accessing https sites using the following call:

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
    ]
);

这篇关于PHP curl_multi_getcontent返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆