-1
메타베이스 엔진을 만들고 JSON 결과를 생성하는 다음 코드를 작성하려고합니다.PHP로 JSON 결과 구문 분석
<?php
$search = $_GET['results'];
if(isset($_GET['results']) && $_GET['results'] != "")
{
echo "<br />Your Search Result Array:<br /><br />";
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=".str_replace(' ', '%20', $_GET['results']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
print_r($json);
이렇게하면 주문이나 하이퍼 링크없이 JSON 정렬되지 않은 결과를 얻을 수 있습니다. PHP를 사용하여 결과를 파싱하려고합니다. 검색 엔진에 "hello"를 입력하면이 메시지가 나타납니다.
stdClass Object ([responseData] => stdClass Object ([results] => Array ([0] =>stdClass Object ([GsearchResultClass] => GwebSearch [unescapedUrl] => http://www.hellomagazine.com/ [url] => http://www.hellomagazine.com/ [visibleUrl] => www.hellomagazine.com [cacheUrl] => http://www.google.com/search?q=cache:QzMhUCC4zBoJ:www.hellomagazine.com [title] => HELLO! Online: celebrity & royal news, magazine, babies,
처음 4 줄 시도했습니다
foreach($results['responsedata']['results']['GsearchResultsClass'] as $result)
{
echo $result['title'].'<br/>';
}
그러나 foreach 라인에 많은 오류가 있습니다.
많은 조언은 JSON에 익숙하지 않으므로 결과를 구문 분석하는 방법에 대한 모든 도움을 환영합니다.
'$ results'는 배열이 아니기 때문에. 'var_dump ($ results);'그리고보십시오. 또한'json_decode()'의 두 번째 매개 변수를 확인하십시오 – zerkms
배열로 액세스하려면 ['json_decode'] (http://php.net/manual/en/function.json-decode.php '$ json = json_decode ($ body, true); ' – Jon
사이드 노트, 왜'$ search'를 사용하지 않고 항상 참조 할 때'$ search = $ _GET ['results '];를 지정합니까? 그것은'$ _GET [ 'results']'입니까? – Jon