2011-03-24 66 views
0

因此,新的谷歌定製搜索已經推出了具有顯示結果麻煩IM ...新的谷歌自定義搜索API的結果 - PHP

我使用PHP來返回JSON的結果和我得到一個無效的參數在每個錯誤...這裏是我使用的是什麼...

$url = "https://www.googleapis.com/customsearch/v1?key=MYKEY&cx=017576662512468239146:omuauf_lfve&q=test&callback=json"; 
$results = file_get_contents($url); 

foreach ($results->items as $r) 
{ 

} 

這裏的回調,當我去到URL在瀏覽器:

json({ 
"kind": "customsearch#search", 
"url": { 
    "type": "application/json", 
    "template": "https://www.googleapis.com/customsearch/v1?q\u003d{searchTerms}&num\u003d{count?}&start\u003d{startIndex?}&hr\u003d{language?}&safe\u003d{safe?}&cx\u003d{cx?}&cref\u003d{cref?}&sort\u003d{sort?}&alt\u003djson" 
}, 
"queries": { 
    "nextPage": [ 
    { 
    "title": "Google Custom Search - test", 
    "totalResults": 276000000, 
    "searchTerms": "test", 
    "count": 10, 
    "startIndex": 11, 
    "inputEncoding": "utf8", 
    "outputEncoding": "utf8", 
    "safe": "off", 
    "cx": "017576662512468239146:omuauf_lfve" 
    } 
    ], 
    "request": [ 
    { 
    "title": "Google Custom Search - test", 
    "totalResults": 276000000, 
    "searchTerms": "test", 
    "count": 10, 
    "startIndex": 1, 
    "inputEncoding": "utf8", 
    "outputEncoding": "utf8", 
    "safe": "off", 
    "cx": "017576662512468239146:omuauf_lfve" 
    } 
    ] 
}, 
"context": { 
    "title": "Curriculum", 
    "facets": [ 
    [ 
    { 
    "label": "lectures", 
    "anchor": "Lectures" 
    } 
    ], 
    [ 
    { 
    "label": "assignments", 
    "anchor": "Assignments" 
    } 
    ], 
    [ 
    { 
    "label": "reference", 
    "anchor": "Reference" 
    } 
    ] 
    ] 
}, 
"items": [ 
    { 
    "kind": "customsearch#result", 
    "title": "Lecture 8: Pseudo Randomness and the Next-bit test 1 Review 2 ...", 
    "htmlTitle": "Lecture 8: Pseudo Randomness and the Next-bit \u003cb\u003etest\u003c/b\u003e 1 Review 2 \u003cb\u003e...\u003c/b\u003e", 
    "link": "http://www.cs.cornell.edu/courses/cs687/2008sp/scribes/scribe19Feb2008.pdf", 
    "displayLink": "www.cs.cornell.edu", 
    "snippet": "Feb 19, 2008 ... Definition 2 An ensemble {Xn} passes the Next Bit test ⇐⇒ ∀ PPT A, ... Next Bit test. We have to now prove the other direction. ...", 
    "htmlSnippet": "Feb 19, 2008 \u003cb\u003e...\u003c/b\u003e Definition 2 An ensemble {Xn} passes the Next Bit \u003cb\u003etest\u003c/b\u003e ⇐⇒ ∀ PPT A, \u003cb\u003e...\u003c/b\u003e Next \u003cbr\u003e Bit \u003cb\u003etest\u003c/b\u003e. We have to now prove the other direction. \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "ErjWs7c3umEJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "producer": "MiKTeX pdfTeX-1.40.4", 
     "creator": "TeX", 
     "creationdate": "D:20080306114145-05'00'", 
     "moddate": "D:20080306114145-05'00'", 
     "fullbanner": "This is MiKTeX-pdfTeX 2.7.2808 (1.40.4)" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "General Information for Computer Science 227 and 228 Test-Out ...", 
    "htmlTitle": "General Information for Computer Science 227 and 228 \u003cb\u003eTest\u003c/b\u003e-Out \u003cb\u003e...\u003c/b\u003e", 
    "link": "http://www.cs.iastate.edu/gradadm/cmarquar/testout.pdf", 
    "displayLink": "www.cs.iastate.edu", 
    "snippet": "should fill out an application for a test-out in the main office and find ... \" Students may ordinarily attempt a CBE test only once in any course or area. ...", 
    "htmlSnippet": "should fill out an application for a \u003cb\u003etest\u003c/b\u003e-out in the main office and find \u003cb\u003e...\u003c/b\u003e "\u003cbr\u003e Students may ordinarily attempt a CBE \u003cb\u003etest\u003c/b\u003e only once in any course or area. \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "HvE3JGhnVkgJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "creationdate": "D:20091006145719-05'00'", 
     "author": "prabhu", 
     "creator": "PScript5.dll Version 5.2.2", 
     "producer": "Acrobat Distiller 7.0 (Windows)", 
     "moddate": "D:20091006145719-05'00'" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "CS1130 Grades/exams/assignments", 
    "htmlTitle": "CS1130 Grades/exams/assignments", 
    "link": "http://www.cs.cornell.edu/courses/cs1130/2010fa/gradesexams.html", 
    "displayLink": "www.cs.cornell.edu", 
    "snippet": "There will be two tests. The exams require mastery of the material: you will be expected to get 85% on each test. If you get lower than 85% on a test, ...", 
    "htmlSnippet": "There will be two \u003cb\u003etests\u003c/b\u003e. The exams require mastery of the material: you will be \u003cbr\u003e expected to get 85% on each \u003cb\u003etest\u003c/b\u003e. If you get lower than 85% on a \u003cb\u003etest\u003c/b\u003e, \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "fjSAJnTr0FgJ" 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "Lecture 16: Recognition II Outline Model-based recognition ...", 
    "htmlTitle": "Lecture 16: Recognition II Outline Model-based recognition \u003cb\u003e...\u003c/b\u003e", 
    "link": "http://www.cs.utexas.edu/~grauman/courses/378/slides/lecture16.pdf", 
    "displayLink": "www.cs.utexas.edu", 
    "snippet": "Hypothesize and test. • Given model of object .... Hypothesize and test: looking for object and ... 「Test」 as function of these representations that ...", 
    "htmlSnippet": "Hypothesize and \u003cb\u003etest\u003c/b\u003e. • Given model of object \u003cb\u003e....\u003c/b\u003e Hypothesize and \u003cb\u003etest\u003c/b\u003e: looking \u003cbr\u003e for object and \u003cb\u003e...\u003c/b\u003e 「\u003cb\u003eTest\u003c/b\u003e」 as function of these representations that \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "_QUgy1DsMFgJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "creationdate": "D:20071108175221-06'00'", 
     "author": "grauman", 
     "creator": "PScript5.dll Version 5.2.2", 
     "producer": "Acrobat Distiller 8.1.0 (Windows)", 
     "moddate": "D:20071108175221-06'00'" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "Notes on the Miller-Rabin randomized primality test", 
    "htmlTitle": "Notes on the Miller-Rabin randomized primality \u003cb\u003etest\u003c/b\u003e", 
    "link": "http://www.cs.cornell.edu/courses/cs482/2008sp/handouts/mrpt.pdf", 
    "displayLink": "www.cs.cornell.edu", 
    "snippet": "Apr 25, 2008 ... test whether a number is prime. It is called the Miller-Rabin primality .... The Miller-Rabin test is based on a third way to prove that a ...", 
    "htmlSnippet": "Apr 25, 2008 \u003cb\u003e...\u003c/b\u003e \u003cb\u003etest\u003c/b\u003e whether a number is prime. It is called the Miller-Rabin primality \u003cb\u003e....\u003c/b\u003e The \u003cbr\u003e Miller-Rabin \u003cb\u003etest\u003c/b\u003e is based on a third way to prove that a \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "IP9TrtclTpAJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "producer": "pdfTeX-1.40.3", 
     "creator": "TeX", 
     "creationdate": "D:20080424173852-04'00'", 
     "moddate": "D:20080424173852-04'00'", 
     "fullbanner": "This is pdfTeX, Version 3.141592-1.40.3-2.2 (Web2C 7.5.6) kpathsea version 3.5.6" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "TOWARD A THEORY OF TEST DATA SELECTION John B. Go0denough Susan L ...", 
    "htmlTitle": "TOWARD A THEORY OF \u003cb\u003eTEST\u003c/b\u003e DATA SELECTION John B. Go0denough Susan L \u003cb\u003e...\u003c/b\u003e", 
    "link": "http://www.cs.umd.edu/class/spring2003/cmsc838p/VandV/criteria.pdf", 
    "displayLink": "www.cs.umd.edu", 
    "snippet": "Formal Definitions and the Fundamental Theorem of Testing criterion is reliable if and only if ... set of test data, i. e. , if it can be shown that for ...", 
    "htmlSnippet": "Formal Definitions and the Fundamental Theorem of \u003cb\u003eTesting\u003c/b\u003e criterion is reliable \u003cbr\u003e if and only if \u003cb\u003e...\u003c/b\u003e set of \u003cb\u003etest\u003c/b\u003e data, i. e. , if it can be shown that for \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "44B1_Z8kNHYJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "moddate": "D:20010115130600-05'00'", 
     "creationdate": "D:20010114103427-05'00'" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "Lecture Notes 16 - pmwiki - Main.homepage", 
    "htmlTitle": "Lecture Notes 16 - pmwiki - Main.homepage", 
    "link": "http://zoo.cs.yale.edu/classes/cs467/2008f/attach/ln16.html", 
    "displayLink": "zoo.cs.yale.edu", 
    "snippet": "Nov 3, 2008 ... If n is prime, the test always fails by Theorem 1 of section 68. ... The test μa (n) is based on computing a sequence b0,b1,…,bs of integers ...", 
    "htmlSnippet": "Nov 3, 2008 \u003cb\u003e...\u003c/b\u003e If n is prime, the \u003cb\u003etest\u003c/b\u003e always fails by Theorem 1 of section 68. \u003cb\u003e...\u003c/b\u003e The \u003cb\u003etest\u003c/b\u003e μa\u003cbr\u003e (n) is based on computing a sequence b0,b1,…,bs of integers \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "TnMAQxlY3HIJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "originator": "TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)", 
     "src": "ln16.tex", 
     "date": "2008-11-11 16:55:00" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "CS 337 Test 3 5/5/06 Open book and notes. Max points \u003d 50 Time ...", 
    "htmlTitle": "CS 337 \u003cb\u003eTest\u003c/b\u003e 3 5/5/06 Open book and notes. Max points \u003d 50 Time \u003cb\u003e...\u003c/b\u003e", 
    "link": "http://www.cs.utexas.edu/users/misra/Classes.dir/337quizSoln.dir/S06.quiz3.pdf", 
    "displayLink": "www.cs.utexas.edu", 
    "snippet": "Test 3. 5/5/06. Open book and notes. Max points \u003d 50. Time \u003d 50 min. Do all questions. 1. (Relational Algebra; 15 points) ...", 
    "htmlSnippet": "\u003cb\u003eTest\u003c/b\u003e 3. 5/5/06. Open book and notes. Max points \u003d 50. Time \u003d 50 min. Do all \u003cbr\u003e questions. 1. (Relational Algebra; 15 points) \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "ClTLaytSRUIJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "creator": "TeX output 2006.05.04:1646", 
     "producer": "dvipdfm 0.13.2c, Copyright Š 1998, by Mark A. Wicks", 
     "creationdate": "D:20060504164645-06'00'" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "EE410 Test Structures & Testing", 
    "htmlTitle": "EE410 \u003cb\u003eTest\u003c/b\u003e Structures & \u003cb\u003eTesting\u003c/b\u003e", 
    "link": "http://www.stanford.edu/class/ee410/TestStructures.pdf", 
    "displayLink": "www.stanford.edu", 
    "snippet": "Test every unique structure at least once, but don't waste time testing ..... Take good care of the testing setup — it's your only one and it cannot ...", 
    "htmlSnippet": "\u003cb\u003eTest\u003c/b\u003e every unique structure at least once, but don't waste time \u003cb\u003etesting\u003c/b\u003e \u003cb\u003e.....\u003c/b\u003e \u003cbr\u003e Take good care of the \u003cb\u003etesting\u003c/b\u003e setup — it's your only one and it cannot \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "YKadfqMlYuIJ", 
    "pagemap": { 
    "metatags": [ 
    { 
     "author": "Krishna Saraswat", 
     "producer": "Mac OS X 10.6.6 Quartz PDFContext", 
     "creator": "Microsoft PowerPoint", 
     "creationdate": "D:20110209003235Z00'00'", 
     "moddate": "D:20110209003235Z00'00'" 
    } 
    ] 
    } 
    }, 
    { 
    "kind": "customsearch#result", 
    "title": "SIP: Interoperability Test Event FAQ", 
    "htmlTitle": "SIP: Interoperability \u003cb\u003eTest\u003c/b\u003e Event FAQ", 
    "link": "http://www.cs.columbia.edu/sip/sipit/faq.html", 
    "displayLink": "www.cs.columbia.edu", 
    "snippet": "\"The interoperability test event is for non-competitve, friendly testing of ... It is meant as someplace people can come to, and test their code at, ...", 
    "htmlSnippet": ""The interoperability \u003cb\u003etest\u003c/b\u003e event is for non-competitve, friendly \u003cb\u003etesting\u003c/b\u003e of \u003cb\u003e...\u003c/b\u003e \u003cbr\u003e It is meant as someplace people can come to, and \u003cb\u003etest\u003c/b\u003e their code at, \u003cb\u003e...\u003c/b\u003e", 
    "cacheId": "m7jgghXfi78J", 
    "pagemap": { 
    "metatags": [ 
    { 
     "author": "Henning Schulzrinne" 
    } 
    ] 
    } 
    } 
] 
} 
); 

回答

0

你應該試試這個:

$results = json_decode(file_get_contents($url));

爲了能夠正確地解碼該服務的響應,你可以做到以下幾點:

$json = file_get_contents($url); 

$start = 'json('; 
$end = ');'; 

$substr_start_pos = strpos($json, $start) + strlen($start); 
$substr_length = strlen($json) - $substr_start_pos - (strlen($json) - strrpos($json, $end)); 

$json = substr($json, $substr_start_pos, $substr_length); 

$results = json_decode($json); 

這可能是一個有點混亂,但我這樣做的原因如下:

  • 你提供的樣本響應有一個'json('字符串在實際的JSON之前,和一個');'之後,它不能用PHP的json_decode解碼。
  • 簡單替換並不好,因爲實際的JSON也可能包含這些子字符串。
+1

您通常想要測試您在將它傳遞給json_decode()之前返回了非空字符串,以避免PHP警告。 – 2011-03-24 17:33:07

+0

你是對的。而且,從file_get_contents獲取來自URL的響應的方法要好得多。我只是展示了這個概念。 – 2011-03-24 17:35:48

+0

你得到一個不好的請求的原因是因爲我沒有在URL中包含我的密鑰,而是使用了「MYKEY」,這顯然不是真正的密鑰。我還將代碼更改爲[code] $ results = json_decode(file_get_contents($ url)); [/ code]並獲取相同的錯誤。 – tytyguy 2011-03-24 18:32:28

0

這是真的簡單的PHP DECODE不會幫你, 試試這個
$data = html_entity_decode($url); 我相信這將正常工作 嘗試以後變種轉儲到檢查結果的日期。