所以我在使用 ampps,然后切换到 z-wamp,认为它可以解决问题,但事实并非如此。
我在我的本地主机(localhost/site1 和 localhost/site2)中有单独的“站点”,我试图向其发送多 curl 请求,但由于某些奇怪的原因,它什么也没做!它仅在我对一个站点进行一次 curl 时才有效。这有效:
$ch = curl_init('http://localhost/site1/');
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array('data' => $data)
));
$res = curl_exec($ch);
//yay!
另一方面,这不起作用:
...
//add a bunch of curl sessions
//to $this->sessions
...
$window = 15;
if (count($this->sessions) < $window)
$window = count($this->sessions);
$mh = curl_multi_init();
$site_map = array();
for ($i = 0; $i < $window; ++$i) {
curl_multi_add_handle($mh, $this->sessions[$i]);
$site_map[(string) $this->sessions[$i]] = $i;
}
$data_results = array();
$running = null;
do {
$execrun = curl_multi_exec($mh, $running);
} while ($execrun === CURLM_CALL_MULTI_PERFORM);
while ($running && $execrun === CURLM_OK) {
//the loop just keeps going forever from here
if (curl_multi_select($mh) !== -1) {
do {
$execrun = curl_multi_exec($mh, $running);
} while ($execrun === CURLM_CALL_MULTI_PERFORM);
}
if ($execrun !== CURLM_OK)
break;
//to here and never enters the loop below
while ($done = curl_multi_info_read($mh)) {
$output = curl_multi_getcontent($done['handle']);
if ($output)
$data_results[$site_map[(string) $done['handle']]] = $output;
else
$data_results[$site_map[(string) $done['handle']]] = null;
if (isset($this->sessions[$i]) && $i < count($this->sessions)) {
curl_multi_add_handle($mh, $this->sessions[$i]);
$site_map[(string) $this->sessions[$i]] = $i;
++$i;
}
unset($site_map[(string) $done['handle']]);
curl_multi_remove_handle($mh, $done['handle']);
curl_close($done['handle']);
}
}
curl_multi_close($mh);
return $data_results;
所以在上面的 multi curl 代码中,它开始运行,将句柄添加到 $mh,一旦执行,它将继续循环,永远不会进入 $done = curl_multi_info_read($mh) while 循环。同时它仍然运行良好并且 $running 始终等于 2。此外,curl_multi_info_read 将返回 false。所以它会一直循环下去。
我的 curl 扩展已启用(显然,如果单个 curl 有效),以下是来自 PHP Info 的详细信息:
cURL support enabled
cURL Information 7.24.0
Age 3
Features
AsynchDNS Yes
Debug No
GSS-Negotiate No
IDN No
IPv6 Yes
Largefile Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI No
krb4 No
libz Yes
CharConv No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host i386-pc-win32
SSL Version OpenSSL/1.0.0g
ZLib Version 1.2.5
libSSH Version libssh2/1.3.0
这到底是怎么回事?它可能与我的 PHP 配置有关吗? Apache 配置?我再次使用 z-wamp。
PHP 版本 5.3.10 Apache 版本 2.4.1 赢 7 64 位 将 PHP 目录添加到 PATH
编辑 事实证明,这一定是某种我根本无法发现的 Apache/PHP 配置类型问题,因为我取消了 z-wamp 并安装了 wampserver,这次它起作用了。
最佳答案
我刚刚回答了另一个关于多个 curl 调用的问题。
这就是我为运行请求所做的全部工作。
do {
$status = curl_multi_exec($mh, $running);
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);
然后我通过遍历我的 curl 处理程序数组来获取我需要的信息。
$returned = array();
foreach ($requests as $identifier => $request) {
$returned[$identifier] = curl_multi_getcontent($request);
curl_multi_remove_handle($mh, $request);
curl_close($request);
}
上述方法对我有用,但您似乎只想向 curl 多处理程序添加一定数量的 session 。我们或许可以将上面的 do-while 循环更改为以下内容:
do {
$status = curl_multi_exec($mh, $running);
if ($running < $window) {
for ($x = 0; $x < $window - $running; $x++) {
$index = count($site_map) + $x -1;
curl_multi_add_handle($mh, $this->sessions[$index]);
$site_map[(string) $this->sessions[$index]] = $index;
}
}
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);
之后我们可以修改数据获取并将整个 while ($running && $execrun === CURLM_OK){} 部分替换为以下内容,因为它只会运行一次所有 curl 调用已处理:
$returned = array();
foreach ($this->sessions as $identifier => $request) {
$returned[$identifier] = curl_multi_getcontent($request);
curl_multi_remove_handle($mh, $request);
curl_close($request);
}
关于php 单 curl 有效但多 curl 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840688/
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
是否可以在PyYAML或Ruby的Psych引擎中禁用创建anchor和引用(并有效地显式列出冗余数据)?也许我在网上搜索时遗漏了一些东西,但在Psych中似乎没有太多可用的选项,而且我也无法确定PyYAML是否允许这样做.基本原理是我必须序列化一些数据并将其以可读的形式传递给一个不是真正的技术同事进行手动验证。有些数据是多余的,但我需要以最明确的方式列出它们以提高可读性(anchor和引用是提高效率的好概念,但不是人类可读性)。Ruby和Python是我选择的工具,但如果有其他一些相当简单的方法来“展开”YAML文档,它可能就可以了。 最佳答案
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
我试图在Ubuntu14.04中使用Curl安装RVM。我运行了以下命令:\curl-sSLhttps://get.rvm.io|bash-sstable出现如下错误:curl:(7)Failedtoconnecttoget.rvm.ioport80:Networkisunreachable非常感谢解决此问题的任何帮助。谢谢 最佳答案 在执行curl之前尝试这个:echoipv4>>~/.curlrc 关于ruby-在Ubuntu14.04中使用Curl安装RVM时出错,我们在Stack
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw
好的,所以我有了我正在使用的应用程序的这种方法,它可以在生产中使用。我的问题为什么这行得通?这是新的Ruby语法吗?defeditload_elements(current_user)unlesscurrent_user.role?(:admin)respond_todo|format|format.json{render:json=>@user}format.xml{render:xml=>@user}format.htmlendrescueActiveRecord::RecordNotFoundrespond_to_not_found(:json,:xml,:html)end
你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva
我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就