我正在尝试理解curl_multi_exec.我在这里复制了一个手册示例.所以我想知道,它是如何工作的?我猜第一个循环发送http请求?但接下来是循环内部循环使用带有看似未记录的标志的函数.
我想同时下载+ = 70 urls + =.
http://www.php.net/manual/en/function.curl-multi-exec.php
<?php
...
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
...
?>
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Curl for PHP.每次获得大约100页的结果需要花费大量时间.对于每个请求我都使用这样的代码
$ch = curl_init();
// get source
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我有什么选择加快速度?
我该如何使用multi_init等>?
我试图访问和下载使用的一些.torrent文件.但没有任何反应,给出https://torrage.comphp curlcurl_error($ch)
$ch = curl_init ('https://torrage.com/torrent/640FE84C613C17F663551D218689A64E8AEBEABE.torrent');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close ($ch);
echo $error;
Run Code Online (Sandbox Code Playgroud)
这给了.
Cannot communicate securely with peer: no common encryption algorithm(s).
Run Code Online (Sandbox Code Playgroud)
如果我尝试像这样的shell
[root@prod1 yum.repos.d]# curl -I https://torrage.com
curl: (35) Cannot communicate securely with peer: no common encryption algorithm(s).
Run Code Online (Sandbox Code Playgroud)
在详细模式下
[root@prod1 yum.repos.d]# curl -v https://torrage.com
* Rebuilt URL to: https://torrage.com/
* Trying 81.17.30.48...
* …Run Code Online (Sandbox Code Playgroud) 是否有任何理智的方式在PHP中异步发出HTTP请求而不丢弃响应?即,类似于AJAX的东西 - PHP脚本启动请求,它是自己的事情,然后,当收到响应时,回调函数/方法或其他脚本处理响应.
我想到了一种方法 - 为每个请求生成一个新的php进程和另一个脚本 - 第二个脚本执行请求,等待响应然后解析数据并做任何应该做的事情,而原始脚本继续生成新进程.不过,我对这种情况下的性能表示怀疑 - 每次必须创建一个新进程必然会有一些性能损失.
目前我正在使用file_get_contents()将GET数据提交到一个站点数组,但是在执行页面时我收到此错误:
致命错误:超出最长执行时间30秒
我真正希望脚本做的就是开始加载网页,然后离开.每个网页最多可能需要5分钟才能完全加载,我不需要它完全加载.
这是我目前拥有的:
foreach($sites as $s) //Create one line to read from a wide array
{
file_get_contents($s['url']); // Send to the shells
}
Run Code Online (Sandbox Code Playgroud)
编辑:为了清除任何混淆,此脚本用于在其他服务器上启动脚本,不返回任何数据.
编辑:我现在正试图使用cURL来做这个技巧,通过设置一秒的超时使其发送数据然后停止.这是我的代码:
$ch = curl_init($s['url']); //load the urls
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); //Only send the data, don't wait.
curl_exec($ch); //Execute
curl_close($ch); //Close it off.
Run Code Online (Sandbox Code Playgroud)
也许我把选项设置错了.在我们发言时,我正在查看一些手册.只是给你一个更新.谢谢你们所有帮助我的人.
编辑:啊,发现了问题.我使用的是CURLOPT_CONNECTTIMEOUT而不是CURLOPT_TIMEOUT.哎呦.
但是现在,脚本没有触发.它们每个都使用ignore_user_abort(TRUE); 所以我无法理解这个问题
哈,划伤那个.现在工作.非常感谢大家
我试图理解为什么cURL的cookie jar文件在我尝试阅读时是空的时候,我一直在靠墙撞击墙.我刚刚发现,如果我调用curl_close()两次而不是一次,我的代码可以工作,但是,我想知道这是否是cURL的错误.
这是一个例子:
curl_close($chInfo['handle']);
var_dump(is_resource($chInfo['handle']));
Run Code Online (Sandbox Code Playgroud)
那输出boolean true.因此,换句话说,尽管我打电话,手柄仍未关闭curl_close().
我的下一个想法是,可能需要一些时间来关闭句柄,所以我sleep()在curl_close()通话后尝试使用了几秒钟,但没有任何区别.
出于绝望,我试着复制这curl_close()条线,像这样:
curl_close($chInfo['handle']);
curl_close($chInfo['handle']);
var_dump(is_resource($chInfo['handle']));
Run Code Online (Sandbox Code Playgroud)
那个输出boolean false,意味着句柄被关闭,我能够从cookie jar文件中读取(当句柄关闭时,cURL将cookie写入文件).
那么这里发生了什么?这似乎非常像一个错误!
编辑:我无法发布我的完整代码(你不会想要阅读它!),但这是一个简化的例子(请注意,在这个例子中只提取了一个网址,而在我的实际代码curl_multi中用于获取同时有很多网址):
$curlOptions = array(
CURLOPT_USERAGENT => 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101',
CURLOPT_CONNECTTIMEOUT => 5, // the number of seconds to wait while trying to connect.
CURLOPT_TIMEOUT => 5, // the maximum number of seconds to allow cURL functions to execute.
CURLOPT_RETURNTRANSFER => 1, // TRUE to …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我从API中提取数据(JSON).我遇到的问题是内存正在慢慢增长,直到我遇到可怕的致命错误:
致命错误:允许的内存大小*字节耗尽(试图分配*字节)在C:...在线*
我不认为应该有任何记忆增长.我尝试在循环结束时取消所有内容,但没有区别.所以我的问题是:我做错了吗?这是正常的吗?我该怎么做才能解决这个问题?
<?php
$start = microtime(true);
$time = microtime(true) - $start;
echo "Start: ". memory_get_peak_usage(true) . " | " . memory_get_usage() . "<br/>";
include ('start.php');
include ('connect.php');
set_time_limit(0);
$api_key = 'API-KEY';
$tier = 'Platinum';
$threads = 10; //number of urls called simultaneously
function multiRequest($urls, $start) {
$time = microtime(true) - $start;
echo " start function: ". memory_get_peak_usage(true) . " | " . memory_get_usage() . "<br>";
$nbrURLS = count($urls); // number of urls in array $urls
$ch = array(); // array …Run Code Online (Sandbox Code Playgroud) 这是我目前的代码:
$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error()); //Query the urls table
while($resultSet = mysql_fetch_array($SQL)){ //Put all the urls into one variable
// Now for some cURL to run it.
$ch = curl_init($resultSet['url']); //load the urls
curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
curl_exec($ch); //Execute
curl_close($ch); //Close it off
} //While loop
Run Code Online (Sandbox Code Playgroud)
我对cURL比较陌生.相对较新,我的意思是这是我第一次使用cURL.目前它加载一个两秒钟,然后加载下一个2秒,然后下一个.但是,我想让它同时加载所有这些.我确信它有可能,我只是不确定如何.如果有人能指出我正确的方向,我会很感激.
我一直在关注如何使用本教程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代码段.我最后关闭了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);
Run Code Online (Sandbox Code Playgroud)
编辑
这是当前可用的代码,但导致页面加载速度太慢
$ch = curl_init();
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); …Run Code Online (Sandbox Code Playgroud) Relevant information: issue 3602 on GitHub
I'm working on a project that gathers and tests public/free proxies, and noticed that when I use the curl_multi interface for testing these proxies, sometimes I get many 28(timeout) errors. This never happens if I test every proxy alone.
The problem is that this issue is unreliably reproducible, and it does not always show up , it could be something in curl or something else.
Unfortunately, I'm not such a deep networks debugger …
curl-multi ×10
php ×10
curl ×8
asynchronous ×1
bug-tracking ×1
c ×1
http ×1
https ×1
json ×1
mysql ×1
request ×1
ssl ×1