谷歌搜索一段时间后,我发现我可以使用这段代码进行谷歌搜索:
$text=$_GET['text'];
$results=file_get_contents("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . $text);
// Too lazy to use cURL
$results=json_decode($results);
问题是 $results 实际上是一个 object(stdClass),像处理数组一样处理它是行不通的(即 $results [“响应数据”])。这是完整的 var_dump:
object(stdClass)#1 (3) {
["responseData"]=>
object(stdClass)#2 (2) {
["results"]=>
array(4) {
[0]=>
object(stdClass)#3 (8) {
["GsearchResultClass"]=>
string(10) "GwebSearch"
["unescapedUrl"]=>
string(51) "http://www.urbandictionary.com/define.php?term=lulz"
["url"]=>
string(55) "http://www.urbandictionary.com/define.php%3Fterm%3Dlulz"
["visibleUrl"]=>
string(23) "www.urbandictionary.com"
["cacheUrl"]=>
string(73) "http://www.google.com/search?q=cache:9eN_paaTATEJ:www.urbandictionary.com"
["title"]=>
string(29) "Urban Dictionary: <b>lulz</b>"
["titleNoFormatting"]=>
string(22) "Urban Dictionary: lulz"
["content"]=>
string(159) "Beginning as a plural variant of lol, <b>Lulz</b> was originally an exclamation but is now often used as a noun meaning interesting or funny internet cont..."
}
[1]=>
object(stdClass)#4 (8) {
["GsearchResultClass"]=>
string(10) "GwebSearch"
["unescapedUrl"]=>
string(34) "http://en.wiktionary.org/wiki/lulz"
["url"]=>
string(34) "http://en.wiktionary.org/wiki/lulz"
["visibleUrl"]=>
string(17) "en.wiktionary.org"
["cacheUrl"]=>
string(67) "http://www.google.com/search?q=cache:ksdO4S_AefUJ:en.wiktionary.org"
["title"]=>
string(24) "<b>lulz</b> - Wiktionary"
["titleNoFormatting"]=>
string(17) "lulz - Wiktionary"
["content"]=>
string(173) "Like “Hakuna matata†in The Lion King, “<b>lulz</b>†is not just a word, but a philosophy. [...] Anonymous has made campaigning sexy for the first time since 1968."
}
[2]=>
object(stdClass)#5 (8) {
["GsearchResultClass"]=>
string(10) "GwebSearch"
["unescapedUrl"]=>
string(51) "http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
["url"]=>
string(51) "http://knowyourmeme.com/memes/i-did-it-for-the-lulz"
["visibleUrl"]=>
string(16) "knowyourmeme.com"
["cacheUrl"]=>
string(66) "http://www.google.com/search?q=cache:ITVRlkQkfckJ:knowyourmeme.com"
["title"]=>
string(45) "I Did It for the <b>Lulz</b> | Know Your Meme"
["titleNoFormatting"]=>
string(38) "I Did It for the Lulz | Know Your Meme"
["content"]=>
string(173) "About I Did it for the <b>Lulz</b> (also known as 4 the <b>lulz</b>) is a popular catchphrase used to express that one carried out a specific action for the sake of perso."
}
[3]=>
object(stdClass)#6 (8) {
["GsearchResultClass"]=>
string(10) "GwebSearch"
["unescapedUrl"]=>
string(56) "http://www.internetslang.com/LULZ-meaning-definition.asp"
["url"]=>
string(56) "http://www.internetslang.com/LULZ-meaning-definition.asp"
["visibleUrl"]=>
string(21) "www.internetslang.com"
["cacheUrl"]=>
string(71) "http://www.google.com/search?q=cache:UNXME3GmS-AJ:www.internetslang.com"
["title"]=>
string(88) "What does <b>LULZ</b> mean? - <b>LULZ</b> Definition - Meaning of <b>LULZ</b> <b>...</b>"
["titleNoFormatting"]=>
string(60) "What does LULZ mean? - LULZ Definition - Meaning of LULZ ..."
["content"]=>
string(185) "This Internet Slang page is designed to explain what the meaning of <b>LULZ</b> is. The slang word / acronym / abbreviation <b>LULZ</b> means... . Internet Slang. A list of <b>...</b>"
}
}
["cursor"]=>
object(stdClass)#7 (6) {
["resultCount"]=>
string(9) "1,080,000"
["pages"]=>
array(8) {
[0]=>
object(stdClass)#8 (2) {
["start"]=>
string(1) "0"
["label"]=>
int(1)
}
[1]=>
object(stdClass)#9 (2) {
["start"]=>
string(1) "4"
["label"]=>
int(2)
}
[2]=>
object(stdClass)#10 (2) {
["start"]=>
string(1) "8"
["label"]=>
int(3)
}
[3]=>
object(stdClass)#11 (2) {
["start"]=>
string(2) "12"
["label"]=>
int(4)
}
[4]=>
object(stdClass)#12 (2) {
["start"]=>
string(2) "16"
["label"]=>
int(5)
}
[5]=>
object(stdClass)#13 (2) {
["start"]=>
string(2) "20"
["label"]=>
int(6)
}
[6]=>
object(stdClass)#14 (2) {
["start"]=>
string(2) "24"
["label"]=>
int(7)
}
[7]=>
object(stdClass)#15 (2) {
["start"]=>
string(2) "28"
["label"]=>
int(8)
}
}
["estimatedResultCount"]=>
string(7) "1080000"
["currentPageIndex"]=>
int(0)
["moreResultsUrl"]=>
string(76) "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=lulz"
["searchResultTime"]=>
string(4) "0.23"
}
}
["responseDetails"]=>
NULL
["responseStatus"]=>
int(200)
}
那么,关于如何读取结果数组有什么线索吗? (除了 $results["responseData"] 没有尝试任何东西,因为我以前从未使用过 JSON。)
最佳答案
使用箭头运算符访问对象的数据:
$results->responseData;
或者,如果您更喜欢数组,您可以使用 json_decode 的第二个参数将 json 数据作为数组返回,例如:
$results = json_decode($results, true);
通常,您还可以使用以下方法将任何对象转换为数组:
$results = (array) $results;
使用以上任一方法,您可以使用 PHP 数组键语法访问您的数据:
$results['responseData'];
关于php - 处理 stdClass 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672561/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。