Jump to content

Google JSON Results into Array


samtwilliams

Recommended Posts

Hi, I am returning some google json results into an array, but i can't seem to loop through the array to get to the result i need.

 

I have the following results

stdClass Object
(
    [kind] => customsearch#search
    [url] => stdClass Object
        (
            [type] => application/json
            [template] => https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json
        )

    [queries] => stdClass Object
        (
            [request] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => Google Custom Search - just eat manchester dragon
                            [totalResults] => 296000
                            [searchTerms] => just eat manchester dragon
                            [count] => 10
                            [startIndex] => 1
                            [inputEncoding] => utf8
                            [outputEncoding] => utf8
                            [safe] => off
                            [cx] => 013036536707430787589:_pqjad5hr1a
                        )

                )

I then use this code to load it into the array;

$google_api = 'https://www.googleapis.com/customsearch/v1?key=<MYKEY>&cx=013036536707430787589:_pqjad5hr1a&q=just+eat+manchester+dragon&alt=json';

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

when i loop through the array i want to pick up the value from 'kind' and i cant seem to get the foreach correct, my last effort was this;

foreach ($results->kind as $detail) {
	echo $detail;
}
	

could someone advise what the best way to pull the results out of the array, like any other array i would navigate through it using -> but when i do this i get an undefined stdClass error.

 

Sam

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.