Avatar of tjyoung
tjyoung
 asked on

How to deal with the returning json from API query from pipl.com

Hi,
Below is a json response I get back from pipl.com (social insight API). The site returns all kinds of info but I'm only after a couple parts with a condition. How can I get at just those parts if it meets my conditions?

I'm interested in retrieving the 'person' shown below if it exists with data. Below it does, otherwise, it simply returns:
person{}

So if there is data in person... ideally I'm after: 'sources', 'jobs' and 'images'

Currently and I'm not sure its the right way to go about:
I receive the json and I use json_decode($reponse); to get something I'm slightly familiar with. Seems like I would need to do some kind of loop to get at each source?

This for instance gets me the first source in the response after I decode it and do a variable dump. So I'm hovering around at the data. Just not sure how to work with it. How to get at each source? That sort of thing.

$insight = json_decode($response);
var_dump($insight->person->sources[0]);

With that in mind, I was thinking something like at while loop where 'x' is the sources[x]. Can't figure out the syntax if that is the right way to go.

{
    "@http_status_code": 200,
    "@records_count": 20,
    "@records_available": 150,
    "warnings": [],
    "query": {
        "names": [
            {
                "first": "John",
                "last": "Doe",
                "display": "John Doe"
            }
        ],
        "addresses": [
            {
                "country": "US",
                "state": "CA",
                "display": "California, US"
            }
        ],
        "emails": [
            {
                "address": "John@mysite.com",
                "address_md5": "561f4b92651c46709fe3ab7b30dde8a6"
            }
        ]
    },
    "person": {
        "@query_params_match": true,
        "sources": [
            {
                "@is_sponsored": false,
                "url": "http://www.twitter.com/johndoe",
                "domain": "twitter.com",
                "name": "Micro Blog - Twitter",
                "category": "personal_profiles"
            },
            {
                "@is_sponsored": false,
                "url": "http://www.linkedin.com/pub/7/318/33",
                "domain": "linkedin.com",
                "name": "Professional Profile & Networking - LinkedIn",
                "category": "personal_profiles"
            }
        ],
        "names": [
            {
                "first": "John",
                "last": "Doe",
                "display": "John Doe"
            }
        ],
        "addresses": [
            {
                "country": "US",
                "state": "CA",
                "city": "California"
            }
        ],
        "emails": [
            {
                "address": "John@mysite.com",
                "address_md5": "561f4b92651c46709fe3ab7b30dde8a6"
            },
            {
                "address_md5": "8b4941f0f407593c1a592b3743dfc4dd"
            }
        ],
        "jobs": [
            {
                "title": "Owner/Developer",
                "organization": "MyApp",
                "display": "Owner/Developer at MyApp"
            },
            {
                "title": "Marketing & Developer",
                "organization": "The Elite Group",
                "industry": "Automotive industry",
                "date_range": {
                    "start": "2008-03-01",
                    "end": "2011-02-05"
                },
                "display": "Marketing & Developer at The Elite Group (2008-2011, Automotive industry)"
            },
            {
                "title": "Marketing & Development",
                "organization": "Musicstop/Long & McQuade",
                "industry": "Music industry",
                "date_range": {
                    "start": "2006-08-01",
                    "end": "2008-03-01"
                },
                "display": "Marketing & Development at Musicstop/Long & McQuade (2006-2008, Music industry)"
            },
            {
                "title": "Prepress Manager",
                "organization": "Wayside Press",
                "industry": "Printing industry",
                "date_range": {
                    "start": "2003-05-01",
                    "end": "2005-07-01"
                },
                "display": "Prepress Manager at Wayside Press (2003-2005, Printing industry)"
            },
            {
                "title": "Marketing and Design",
                "organization": "Sport Mart Discount Superstores",
                "industry": "Sporting Goods industry",
                "date_range": {
                    "start": "1996-07-01",
                    "end": "2003-03-01"
                },
                "display": "Marketing and Design at Sport Mart Discount Superstores (1996-2003, Sporting Goods industry)"
            }
        ],
        "images": [
            {
                "url": "http://a0.twimg.com/profile_images/1642851159/MyApp_logo.png"
            },
            {
                "url": "http://media.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/068/243/2655a7.jpg"
            }
        ],
        "usernames": [
            {
                "content": "MyApp"
            }
        ],
        "user_ids": [
            {
                "content": "326804197"
            },
            {
                "content": "21682987"
            },
            {
                "content": "7/318/37"
            }
        ],
        "related_urls": [
            {
                "content": "http://www.MyApp.ca"
            }
        ],
        "tags": [
            {
                "@classification": "About Me",
                "content": "Automotive Marketing"
            },
            {
                "@classification": "Interest",
                "content": "career opportunities"
            },
            {
                "@classification": "Interest",
                "content": "consulting offers"
            },
            {
                "@classification": "Interest",
                "content": "new ventures"
            }
        ]
    },
    "records": [
        {
            "@query_params_match": false,
            "@query_person_match": 1.0,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.twitter.com/MyApp",
                "domain": "twitter.com",
                "name": "Micro Blog - Twitter",
                "category": "personal_profiles"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "CA",
                    "state": "NS",
                    "city": "Halifax",
                    "display": "Halifax, NS, CA"
                }
            ],
            "images": [
                {
                    "url": "http://a0.twimg.com/profile_images/1642851159/MyApp_logo.png"
                }
            ],
            "usernames": [
                {
                    "content": "MyApp"
                }
            ],
            "user_ids": [
                {
                    "content": "326804197"
                }
            ],
            "related_urls": [
                {
                    "content": "http://www.MyApp.ca"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 1.0,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.linkedin.com/pub/7/318/37",
                "domain": "linkedin.com",
                "name": "Professional Profile & Networking - LinkedIn",
                "category": "personal_profiles"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "CA",
                    "city": "Halifax",
                    "display": "Halifax, Canada"
                }
            ],
            "jobs": [
                {
                    "title": "Owner/Developer",
                    "organization": "MyApp",
                    "display": "Owner/Developer at MyApp"
                },
                {
                    "title": "Marketing & Developer",
                    "organization": "The Elite Group",
                    "industry": "Automotive industry",
                    "date_range": {
                        "start": "2008-03-01",
                        "end": "2011-02-05"
                    },
                    "display": "Marketing & Developer at The Elite Group (2008-2011, Automotive industry)"
                },
                {
                    "title": "Marketing & Development",
                    "organization": "Musicstop/Long & McQuade",
                    "industry": "Music industry",
                    "date_range": {
                        "start": "2006-08-01",
                        "end": "2008-03-01"
                    },
                    "display": "Marketing & Development at Musicstop/Long & McQuade (2006-2008, Music industry)"
                },
                {
                    "title": "Prepress Manager",
                    "organization": "Wayside Press",
                    "industry": "Printing industry",
                    "date_range": {
                        "start": "2003-05-01",
                        "end": "2005-07-01"
                    },
                    "display": "Prepress Manager at Wayside Press (2003-2005, Printing industry)"
                },
                {
                    "title": "Marketing and Design",
                    "organization": "Sport Mart Discount Superstores",
                    "industry": "Sporting Goods industry",
                    "date_range": {
                        "start": "1996-07-01",
                        "end": "2003-03-01"
                    },
                    "display": "Marketing and Design at Sport Mart Discount Superstores (1996-2003, Sporting Goods industry)"
                }
            ],
            "images": [
                {
                    "url": "http://media.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/068/243/2655a67.jpg"
                }
            ],
            "user_ids": [
                {
                    "content": "21682987"
                },
                {
                    "content": "7/318/37"
                }
            ],
            "tags": [
                {
                    "@classification": "About Me",
                    "content": "Automotive Marketing"
                },
                {
                    "@classification": "Interest",
                    "content": "career opportunities"
                },
                {
                    "@classification": "Interest",
                    "content": "consulting offers"
                },
                {
                    "@classification": "Interest",
                    "content": "new ventures"
                },
                {
                    "@classification": "Interest",
                    "content": "business deals"
                },
                {
                    "@classification": "Interest",
                    "content": "getting back in touch"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_9341991.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "KY",
                    "city": "Cecilia",
                    "display": "Cecilia, KY, US"
                }
            ],
            "jobs": [
                {
                    "title": "Head Coach",
                    "organization": "Hardin County Schools",
                    "display": "Head Coach at Hardin County Schools"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, Cecilia, KY, Hardin County Schools"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_9417986.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "IA",
                    "city": "Clarksville",
                    "display": "Clarksville, IA, US"
                }
            ],
            "jobs": [
                {
                    "title": "Principal",
                    "organization": "Clarksville Elementary School",
                    "display": "Principal at Clarksville Elementary School"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, t***@clarksville.k12.ia.us, Clarksville, IA, Clarksville Elementary School"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "CA",
                    "city": "Granada Hills",
                    "display": "Granada Hills, CA, US"
                },
                {
                    "country": "US",
                    "state": "WI",
                    "display": "Wisconsin, US"
                }
            ],
            "tags": [
                {
                    "content": "Re: Verdun Doe/Janet Breen. Posted on: 12 Mar 2007, by. . tmkeeps05. Localities North America United States States Wisconsin Counties Marathon ... Schofield, and Tom (Linda) Doe ... six grandchildren, Terry Doe II John Doe. Tom Doe, Jr. Tim Doe, Tiffany Doe, Tara Doe; and one great grandson"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "KY",
                    "display": "Kentucky, US"
                },
                {
                    "country": "US",
                    "state": "TN",
                    "city": "Memphis",
                    "display": "Memphis, TN, US"
                }
            ],
            "tags": [
                {
                    "content": "Buren Garten Jr. Posted on: 19 Dec 2000, by. . akygirl2. Localities North America United States States Kentucky Counties Woodford. a founder of the Bluegrass Wildwater Association. In addition to his wife, he is survived by a son John Doe. of Memphis ... two daughters, Lynna Jo Nicholas, Oak Ridge, Tenn and J D. Garten of Lexingt"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_8620391.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "UT",
                    "city": "Holladay",
                    "display": "Holladay, UT, US"
                }
            ],
            "jobs": [
                {
                    "title": "Vice President of Information Technology",
                    "organization": "JetBlue Airways",
                    "display": "Vice President of Information Technology at JetBlue Airways"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, Holladay, UT, JetBlue Airways"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=OzlErZ1WcKQ&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/OzlErZ1WcKQ/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Colbie Caillat cover song - Diana performing I Never Told You with John"
                },
                {
                    "content": "I Never Told You - Colbie Caillat cover by Diana Perkins and John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=gUQdHSoit5k&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/gUQdHSoit5k/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "John Doe is busy training for this coming racing season. Reigning Extremity Games Silver medalist, and Adaptive X Games silver medalist, he is looking t"
                },
                {
                    "content": "John Doe 2011 promo"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/101599843028312014412/10313?authkey=yYVZs9o0yv0#5930702269116364434",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh5.ggpht.com/-Iu4f_ivLQOk/Uk4WSqNItpI/AAAAAAAAAtE/Bqw1oSj56vI/IMAG0016.jpg"
                }
            ],
            "tags": [
                {
                    "content": "101599843028312014412 (John Doe)"
                },
                {
                    "content": "IMAG0016.jpg"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=pthFlSveAO0&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/pthFlSveAO0/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "John Doe performing his original - Painkillers and Whiskey. John is in the band 2am Logic, check them out at: http://www.myspace.com/2amlogic http://www"
                },
                {
                    "content": "Painkillers and Whiskey - original song by John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=V752LVCF8Gk&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/V752LVCF8Gk/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "3 beds 2 baths John Doe CENTURY 21 Wright-Pace Real Estate http://www.century21.com/property/2702-Graystone-Paragould-AR-72450-37697883"
                },
                {
                    "content": "Homes for Sale - 2702 Graystone Dr Paragould AR 72450 - John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/117315566337101422559/ProfilePhotos?authkey=EMy0dxYTmoc#5933759047647690018",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh3.ggpht.com/-Fjxbru_iK5w/UljyalnM_SI/AAAAAAAAAGc/jtuCZeoQQgs/NMCOA2K15-7.jpg"
                }
            ],
            "tags": [
                {
                    "content": "117315566337101422559 (John Doe)"
                },
                {
                    "content": "NMCOA2K15-7.jpg"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/110673768003000772479/ProfilePhotos?authkey=LsYB12mw4zM#5937673774094327986",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh4.ggpht.com/-qO7Cp-JTKY4/Umba1qELoLI/AAAAAAAAAAk/1Wi60emUiLI/5f2df8ad-e53c-41e5-ad25-5feca59a0cc2.jpg"
                }
            ],
            "tags": [
                {
                    "content": "110673768003000772479 (John Doe)"
                },
                {
                    "content": "5f2df8ad-e53c-41e5-ad25-5feca59a0cc2"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "NC",
                    "display": "North Carolina, US"
                }
            ],
            "tags": [
                {
                    "content": "Re: Thomas John of Rowan Co. Posted on: 02 Sep 2002, by. . NancyMorris61. Localities North America United States States North Carolina Counties Rowan. The parents of the Thomas John who died in 1869 were John John, Jr ... May 1796) and Ann Brandon John Doe ... 17 Oct 1821). Ann Doe is also buried at Thyatira, in the same row with Thomas. There is i"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "NM",
                    "display": "New Mexico, US"
                }
            ],
            "tags": [
                {
                    "content": "Olaf 12-18-1922 to 1-11-2001. Posted on: 16 Jan 2001, by. PJ. Localities North America United States States New Mexico Counties Chaves ... in Los Angeles. She passed away in 2000. Survivors include two sons, Eric Doe of Roswell and John Doe. of Albuquerque; one daughter, Claudia Shigemitsu of Whittier, Calif. ; three sisters, Ruth Soll"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/sirtt/video/dancing.../5411249",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a1-videos.myspacecdn.com/videos02/208/880e54c4f0b8424e9e149a2c76524bfd/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "dancing John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.metacafe.com/watch/mv-8t45j/snowmen_film_dont_waste_this_chance/",
                "domain": "metacafe.com",
                "name": "Videos - Metacafe",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://s.mcstatic.com/thumb/7139922/19768984/4/catalog_item5/0/1/snowmen_film_dont_waste_this_chance.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Snowmen (film) - Don't Waste This Chance. Topics:. Bobb'e J Doe Bobby Coleman Christian Martyn Stephen A Marinaccio II David Segel John Burns. Bill O'Kane. From the movie Snowmen - Billy (Bobby Coleman) leads a battalion of kids in ... Added 9/2/11 By MovieClips com"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/muscledaddy08/video/beast-and-the-harlot-gh2-tyler-Doe-expert/38615461",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a3-videos.myspacecdn.com/videos02/203/0880e2eab040474eab5377430ec1075c/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Beast and the Harlot Gh2 Tyler Doe Expert John Crow"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/JohngDoe/video/more-brandon/10477651",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a1-videos.myspacecdn.com/videos02/201/e19c34601489478c8c50bade26351fb6/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "More Brandon John Doe"
                }
            ]
        }
    ],
    "suggested_searches": []
}

Open in new window

PHPJavaScript

Avatar of undefined
Last Comment
Mark Brady

8/22/2022 - Mon
leakim971

   $insight = json_decode($response); 
   $jsonSources = json_decode($insight, true)["person"]["sources"];

    foreach ($jsonSources as $source) {
        $is_sponsored = $source["@is_sponsored"];
        $url = $source["url"];
        $domain = $source["domain"];
        $name = $source["name"];
        $categorie = $source["categorie"];
        echo $url . " : " . $domain . "<br />";
    }

Open in new window


A good online tools : http://jsonviewer.stack.hu/
tjyoung

ASKER
Hi,
Thanks for the note: This line is keeping the page itself from loading:

$jsonSources = json_decode($insight, true)["person"]["sources"];

any idea what would cause that? I take it out leaving the rest in and no problem loading the page. Also do you have to decode twice?

$insight = json_decode($response);
$jsonSources = json_decode($insight, true)["person"]["sources"];
leakim971

No, you right, just this :
   $jsonSources = json_decode($response)["person"]["sources"]; 

    foreach ($jsonSources as $source) {
        $is_sponsored = $source["@is_sponsored"];
        $url = $source["url"];
        $domain = $source["domain"];
        $name = $source["name"];
        $categorie = $source["categorie"];
        echo $url . " : " . $domain . "<br />";
    }

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
Mark Brady

It might be easier to decode the json response into an associative array. I have always prefered working with arrays over objects. Any change you can post an actual Json response (don't dump it just post the string) ?

Anyway here is how you would do it.

<?php

$insight = json_decode($response, true); // an associative array


foreach ($response as $record) {
    
    if (!isset($record['person']) || empty($record['person'])) {

        // jump out here because "person" is empty
        continue; // this will go to the next record in the loop
    }

    // if you get here then you have data in "person"
    
    // deal with "sources"
    if (isset($record['sources']) && !empty($record['sources'])) {

        // loop each record in sources
        foreach ($record['sources'] as $source) {

            // not sure what you want to do with the data so I'm going to echo it out for now
            echo $record['sources']['@is_sponsored'] ? 'This person is sponsored<br />' : 'This person is not sponsored<br />';
            echo 'URL : ' . $record['sources']['url'] . '<br />';
            echo 'Domain : ' . $record['sources']['domain'] . '<br />';
            echo 'Name : ' . $record['sources']['name'] . '<br />';
            echo 'Category : ' . $record['sources']['category'] . '<br />';

        }
    
    }
    
    // deal with "jobs"
    
    // note: jobs has a different amount of keys per record in you examplae so you would be better to loop this one slightly different.
    if (isset($record['jobs']) && !empty($record['jobs'])) {

        // loop each record in jobs
        foreach ($record['jobs'] as $key => $value) {
            
            echo ucfirst($key). ' : ' . $value . '<br />'; // this one line will deal with all key => value pairs
            
        }
    }
    
    // deal with "images"
    if (isset($record['images']) && !empty($record['images'])) {

        // loop each record in images
        foreach ($record['images'] as $image) {
            echo 'URL : ' . $image['url'] . '<br />';
        }
    }
}

// now you have printed out everything you wanted in the response. I am guessing you don't just want to print it out but you can modify this to suit.
?>

Open in new window

Mark Brady

$jsonSources = json_decode($insight, true)["person"]["sources"];

that line is not valid php
tjyoung

ASKER
Hi Elvin66 - this looks great. Addresses the conditions which is fab.
Have an error on this line:

      foreach ($response as $record) {

Did you mean:

      foreach ($insight as $record) {
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Sorry for the long post, but it's a large amount of test data.  Skip past the JSON string and go right to line 910.  There you will find the first thing you should do whenever you have to deal with external data like this JSON string.  Once you have a visual on the contents of the data set, it's easy to write the code you want, building it up step-by-step.

<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28307343.html


$jso = <<<EOD
{
    "@http_status_code": 200,
    "@records_count": 20,
    "@records_available": 150,
    "warnings": [],
    "query": {
        "names": [
            {
                "first": "John",
                "last": "Doe",
                "display": "John Doe"
            }
        ],
        "addresses": [
            {
                "country": "US",
                "state": "CA",
                "display": "California, US"
            }
        ],
        "emails": [
            {
                "address": "John@mysite.com",
                "address_md5": "561f4b92651c46709fe3ab7b30dde8a6"
            }
        ]
    },
    "person": {
        "@query_params_match": true,
        "sources": [
            {
                "@is_sponsored": false,
                "url": "http://www.twitter.com/johndoe",
                "domain": "twitter.com",
                "name": "Micro Blog - Twitter",
                "category": "personal_profiles"
            },
            {
                "@is_sponsored": false,
                "url": "http://www.linkedin.com/pub/7/318/33",
                "domain": "linkedin.com",
                "name": "Professional Profile & Networking - LinkedIn",
                "category": "personal_profiles"
            }
        ],
        "names": [
            {
                "first": "John",
                "last": "Doe",
                "display": "John Doe"
            }
        ],
        "addresses": [
            {
                "country": "US",
                "state": "CA",
                "city": "California"
            }
        ],
        "emails": [
            {
                "address": "John@mysite.com",
                "address_md5": "561f4b92651c46709fe3ab7b30dde8a6"
            },
            {
                "address_md5": "8b4941f0f407593c1a592b3743dfc4dd"
            }
        ],
        "jobs": [
            {
                "title": "Owner/Developer",
                "organization": "MyApp",
                "display": "Owner/Developer at MyApp"
            },
            {
                "title": "Marketing & Developer",
                "organization": "The Elite Group",
                "industry": "Automotive industry",
                "date_range": {
                    "start": "2008-03-01",
                    "end": "2011-02-05"
                },
                "display": "Marketing & Developer at The Elite Group (2008-2011, Automotive industry)"
            },
            {
                "title": "Marketing & Development",
                "organization": "Musicstop/Long & McQuade",
                "industry": "Music industry",
                "date_range": {
                    "start": "2006-08-01",
                    "end": "2008-03-01"
                },
                "display": "Marketing & Development at Musicstop/Long & McQuade (2006-2008, Music industry)"
            },
            {
                "title": "Prepress Manager",
                "organization": "Wayside Press",
                "industry": "Printing industry",
                "date_range": {
                    "start": "2003-05-01",
                    "end": "2005-07-01"
                },
                "display": "Prepress Manager at Wayside Press (2003-2005, Printing industry)"
            },
            {
                "title": "Marketing and Design",
                "organization": "Sport Mart Discount Superstores",
                "industry": "Sporting Goods industry",
                "date_range": {
                    "start": "1996-07-01",
                    "end": "2003-03-01"
                },
                "display": "Marketing and Design at Sport Mart Discount Superstores (1996-2003, Sporting Goods industry)"
            }
        ],
        "images": [
            {
                "url": "http://a0.twimg.com/profile_images/1642851159/MyApp_logo.png"
            },
            {
                "url": "http://media.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/068/243/2655a7.jpg"
            }
        ],
        "usernames": [
            {
                "content": "MyApp"
            }
        ],
        "user_ids": [
            {
                "content": "326804197"
            },
            {
                "content": "21682987"
            },
            {
                "content": "7/318/37"
            }
        ],
        "related_urls": [
            {
                "content": "http://www.MyApp.ca"
            }
        ],
        "tags": [
            {
                "@classification": "About Me",
                "content": "Automotive Marketing"
            },
            {
                "@classification": "Interest",
                "content": "career opportunities"
            },
            {
                "@classification": "Interest",
                "content": "consulting offers"
            },
            {
                "@classification": "Interest",
                "content": "new ventures"
            }
        ]
    },
    "records": [
        {
            "@query_params_match": false,
            "@query_person_match": 1.0,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.twitter.com/MyApp",
                "domain": "twitter.com",
                "name": "Micro Blog - Twitter",
                "category": "personal_profiles"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "CA",
                    "state": "NS",
                    "city": "Halifax",
                    "display": "Halifax, NS, CA"
                }
            ],
            "images": [
                {
                    "url": "http://a0.twimg.com/profile_images/1642851159/MyApp_logo.png"
                }
            ],
            "usernames": [
                {
                    "content": "MyApp"
                }
            ],
            "user_ids": [
                {
                    "content": "326804197"
                }
            ],
            "related_urls": [
                {
                    "content": "http://www.MyApp.ca"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 1.0,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.linkedin.com/pub/7/318/37",
                "domain": "linkedin.com",
                "name": "Professional Profile & Networking - LinkedIn",
                "category": "personal_profiles"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "CA",
                    "city": "Halifax",
                    "display": "Halifax, Canada"
                }
            ],
            "jobs": [
                {
                    "title": "Owner/Developer",
                    "organization": "MyApp",
                    "display": "Owner/Developer at MyApp"
                },
                {
                    "title": "Marketing & Developer",
                    "organization": "The Elite Group",
                    "industry": "Automotive industry",
                    "date_range": {
                        "start": "2008-03-01",
                        "end": "2011-02-05"
                    },
                    "display": "Marketing & Developer at The Elite Group (2008-2011, Automotive industry)"
                },
                {
                    "title": "Marketing & Development",
                    "organization": "Musicstop/Long & McQuade",
                    "industry": "Music industry",
                    "date_range": {
                        "start": "2006-08-01",
                        "end": "2008-03-01"
                    },
                    "display": "Marketing & Development at Musicstop/Long & McQuade (2006-2008, Music industry)"
                },
                {
                    "title": "Prepress Manager",
                    "organization": "Wayside Press",
                    "industry": "Printing industry",
                    "date_range": {
                        "start": "2003-05-01",
                        "end": "2005-07-01"
                    },
                    "display": "Prepress Manager at Wayside Press (2003-2005, Printing industry)"
                },
                {
                    "title": "Marketing and Design",
                    "organization": "Sport Mart Discount Superstores",
                    "industry": "Sporting Goods industry",
                    "date_range": {
                        "start": "1996-07-01",
                        "end": "2003-03-01"
                    },
                    "display": "Marketing and Design at Sport Mart Discount Superstores (1996-2003, Sporting Goods industry)"
                }
            ],
            "images": [
                {
                    "url": "http://media.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/068/243/2655a67.jpg"
                }
            ],
            "user_ids": [
                {
                    "content": "21682987"
                },
                {
                    "content": "7/318/37"
                }
            ],
            "tags": [
                {
                    "@classification": "About Me",
                    "content": "Automotive Marketing"
                },
                {
                    "@classification": "Interest",
                    "content": "career opportunities"
                },
                {
                    "@classification": "Interest",
                    "content": "consulting offers"
                },
                {
                    "@classification": "Interest",
                    "content": "new ventures"
                },
                {
                    "@classification": "Interest",
                    "content": "business deals"
                },
                {
                    "@classification": "Interest",
                    "content": "getting back in touch"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_9341991.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "KY",
                    "city": "Cecilia",
                    "display": "Cecilia, KY, US"
                }
            ],
            "jobs": [
                {
                    "title": "Head Coach",
                    "organization": "Hardin County Schools",
                    "display": "Head Coach at Hardin County Schools"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, Cecilia, KY, Hardin County Schools"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_9417986.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "IA",
                    "city": "Clarksville",
                    "display": "Clarksville, IA, US"
                }
            ],
            "jobs": [
                {
                    "title": "Principal",
                    "organization": "Clarksville Elementary School",
                    "display": "Principal at Clarksville Elementary School"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, t***@clarksville.k12.ia.us, Clarksville, IA, Clarksville Elementary School"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "CA",
                    "city": "Granada Hills",
                    "display": "Granada Hills, CA, US"
                },
                {
                    "country": "US",
                    "state": "WI",
                    "display": "Wisconsin, US"
                }
            ],
            "tags": [
                {
                    "content": "Re: Verdun Doe/Janet Breen. Posted on: 12 Mar 2007, by. . tmkeeps05. Localities North America United States States Wisconsin Counties Marathon ... Schofield, and Tom (Linda) Doe ... six grandchildren, Terry Doe II John Doe. Tom Doe, Jr. Tim Doe, Tiffany Doe, Tara Doe; and one great grandson"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "KY",
                    "display": "Kentucky, US"
                },
                {
                    "country": "US",
                    "state": "TN",
                    "city": "Memphis",
                    "display": "Memphis, TN, US"
                }
            ],
            "tags": [
                {
                    "content": "Buren Garten Jr. Posted on: 19 Dec 2000, by. . akygirl2. Localities North America United States States Kentucky Counties Woodford. a founder of the Bluegrass Wildwater Association. In addition to his wife, he is survived by a son John Doe. of Memphis ... two daughters, Lynna Jo Nicholas, Oak Ridge, Tenn and J D. Garten of Lexingt"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.lead411.com/John_Doe_8620391.html",
                "domain": "lead411.com",
                "name": "Executive & Business Information - Lead411",
                "category": "professional_and_business"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "UT",
                    "city": "Holladay",
                    "display": "Holladay, UT, US"
                }
            ],
            "jobs": [
                {
                    "title": "Vice President of Information Technology",
                    "organization": "JetBlue Airways",
                    "display": "Vice President of Information Technology at JetBlue Airways"
                }
            ],
            "tags": [
                {
                    "content": "John Doe, Holladay, UT, JetBlue Airways"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=OzlErZ1WcKQ&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/OzlErZ1WcKQ/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Colbie Caillat cover song - Diana performing I Never Told You with John"
                },
                {
                    "content": "I Never Told You - Colbie Caillat cover by Diana Perkins and John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=gUQdHSoit5k&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/gUQdHSoit5k/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "John Doe is busy training for this coming racing season. Reigning Extremity Games Silver medalist, and Adaptive X Games silver medalist, he is looking t"
                },
                {
                    "content": "John Doe 2011 promo"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/101599843028312014412/10313?authkey=yYVZs9o0yv0#5930702269116364434",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh5.ggpht.com/-Iu4f_ivLQOk/Uk4WSqNItpI/AAAAAAAAAtE/Bqw1oSj56vI/IMAG0016.jpg"
                }
            ],
            "tags": [
                {
                    "content": "101599843028312014412 (John Doe)"
                },
                {
                    "content": "IMAG0016.jpg"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=pthFlSveAO0&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/pthFlSveAO0/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "John Doe performing his original - Painkillers and Whiskey. John is in the band 2am Logic, check them out at: http://www.myspace.com/2amlogic http://www"
                },
                {
                    "content": "Painkillers and Whiskey - original song by John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.youtube.com/watch?v=V752LVCF8Gk&feature=youtube_gdata",
                "domain": "youtube.com",
                "name": "Video - YouTube",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://i.ytimg.com/vi/V752LVCF8Gk/1.jpg"
                }
            ],
            "tags": [
                {
                    "content": "3 beds 2 baths John Doe CENTURY 21 Wright-Pace Real Estate http://www.century21.com/property/2702-Graystone-Paragould-AR-72450-37697883"
                },
                {
                    "content": "Homes for Sale - 2702 Graystone Dr Paragould AR 72450 - John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/117315566337101422559/ProfilePhotos?authkey=EMy0dxYTmoc#5933759047647690018",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh3.ggpht.com/-Fjxbru_iK5w/UljyalnM_SI/AAAAAAAAAGc/jtuCZeoQQgs/NMCOA2K15-7.jpg"
                }
            ],
            "tags": [
                {
                    "content": "117315566337101422559 (John Doe)"
                },
                {
                    "content": "NMCOA2K15-7.jpg"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://picasaweb.google.com/110673768003000772479/ProfilePhotos?authkey=LsYB12mw4zM#5937673774094327986",
                "domain": "picasaweb.google.com",
                "name": "Web Album Photo - Picasa",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://lh4.ggpht.com/-qO7Cp-JTKY4/Umba1qELoLI/AAAAAAAAAAk/1Wi60emUiLI/5f2df8ad-e53c-41e5-ad25-5feca59a0cc2.jpg"
                }
            ],
            "tags": [
                {
                    "content": "110673768003000772479 (John Doe)"
                },
                {
                    "content": "5f2df8ad-e53c-41e5-ad25-5feca59a0cc2"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "NC",
                    "display": "North Carolina, US"
                }
            ],
            "tags": [
                {
                    "content": "Re: Thomas John of Rowan Co. Posted on: 02 Sep 2002, by. . NancyMorris61. Localities North America United States States North Carolina Counties Rowan. The parents of the Thomas John who died in 1869 were John John, Jr ... May 1796) and Ann Brandon John Doe ... 17 Oct 1821). Ann Doe is also buried at Thyatira, in the same row with Thomas. There is i"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://boards.ancestry.com/SearchResults.aspx?db=mb&gss=ancMB&hc=25&rank=0&adv=show&p=localities.northam.usa.states.tennessee.counties.mcminn&csn=McMinn&cst=board&gskw=\"John+Doe\"&psrch=&_F00029CB=&_F00027E2=&period=&_80004003=&_F0002BF1=",
                "domain": "boards.ancestry.com",
                "name": "Message Boards Search - Ancestry",
                "category": "public_records"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "addresses": [
                {
                    "country": "US",
                    "state": "NM",
                    "display": "New Mexico, US"
                }
            ],
            "tags": [
                {
                    "content": "Olaf 12-18-1922 to 1-11-2001. Posted on: 16 Jan 2001, by. PJ. Localities North America United States States New Mexico Counties Chaves ... in Los Angeles. She passed away in 2000. Survivors include two sons, Eric Doe of Roswell and John Doe. of Albuquerque; one daughter, Claudia Shigemitsu of Whittier, Calif. ; three sisters, Ruth Soll"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/sirtt/video/dancing.../5411249",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a1-videos.myspacecdn.com/videos02/208/880e54c4f0b8424e9e149a2c76524bfd/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "dancing John Doe"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "http://www.metacafe.com/watch/mv-8t45j/snowmen_film_dont_waste_this_chance/",
                "domain": "metacafe.com",
                "name": "Videos - Metacafe",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "http://s.mcstatic.com/thumb/7139922/19768984/4/catalog_item5/0/1/snowmen_film_dont_waste_this_chance.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Snowmen (film) - Don't Waste This Chance. Topics:. Bobb'e J Doe Bobby Coleman Christian Martyn Stephen A Marinaccio II David Segel John Burns. Bill O'Kane. From the movie Snowmen - Billy (Bobby Coleman) leads a battalion of kids in ... Added 9/2/11 By MovieClips com"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/muscledaddy08/video/beast-and-the-harlot-gh2-tyler-Doe-expert/38615461",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a3-videos.myspacecdn.com/videos02/203/0880e2eab040474eab5377430ec1075c/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "Beast and the Harlot Gh2 Tyler Doe Expert John Crow"
                }
            ]
        },
        {
            "@query_params_match": false,
            "@query_person_match": 0.98405,
            "source": {
                "@is_sponsored": false,
                "url": "https://www.myspace.com/JohngDoe/video/more-brandon/10477651",
                "domain": "myspace.com",
                "name": "Videos - MySpace",
                "category": "media"
            },
            "names": [
                {
                    "first": "John",
                    "last": "Doe",
                    "display": "John Doe"
                }
            ],
            "images": [
                {
                    "url": "https://a1-videos.myspacecdn.com/videos02/201/e19c34601489478c8c50bade26351fb6/300w.jpg"
                }
            ],
            "tags": [
                {
                    "content": "More Brandon John Doe"
                }
            ]
        }
    ],
    "suggested_searches": []
}
EOD;

// MAKE AN OBJECT AND VISUALIZE IT
$obj = json_decode($jso);
echo '<pre>';
var_dump($obj);

Open in new window

tjyoung

ASKER
Hi,
on Elvin66's when run I get:

Message: Invalid argument supplied for foreach()

Filename: controllers/leads.php

Line Number: 119

which corresponds to this line:
foreach ($response as $record) {

The entire code block I'm using including curl is:
	$curl = curl_init();
	curl_setopt_array($curl, array(
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_URL => 'http://api.pipl.com/search/v3/json/?email='.$email.'&first_name='.$firstname.'&last_name='.$lastname.'&city='.$city.'&state='.$state.'&country=CA&key=mykey&pretty=true',
	CURLOPT_TIMEOUT => 50,
	CURLOPT_CONNECTTIMEOUT => 0,
	CURLOPT_HEADER => 'false'
	));		
			
	$response = curl_exec($curl);
	
	curl_close($curl);
	
	$insight = json_decode($response, true); // an associative array
//var_dump($insight);

foreach ($response as $record) {
	
	if (!isset($record['person']) || empty($record['person'])) {
		continue; // this will go to the next record in the loop
    }

    if (isset($record['sources']) && !empty($record['sources'])) {
		
		// loop each record in sources
        foreach ($record['sources'] as $source) {

            // not sure what you want to do with the data so I'm going to echo it out for now
            echo 'URL : ' . $record['sources']['url'] . '<br />';
            echo 'Domain : ' . $record['sources']['domain'] . '<br />';
            echo 'Name : ' . $record['sources']['name'] . '<br />';
            echo 'Category : ' . $record['sources']['category'] . '<br />';
        }
    
    }
    
    // deal with "jobs"
    
    // note: jobs has a different amount of keys per record in you examplae so you would be better to loop this one slightly different.
    if (isset($record['jobs']) && !empty($record['jobs'])) {

        // loop each record in jobs
        foreach ($record['jobs'] as $key => $value) {
            
            echo ucfirst($key). ' : ' . $value . '<br />'; // this one line will deal with all key => value pairs
            
        }
    }
    
    // deal with "images"
    if (isset($record['images']) && !empty($record['images'])) {

        // loop each record in images
        foreach ($record['images'] as $image) {
            echo 'URL : ' . $image['url'] . '<br />';
        }
    }
}

Open in new window

I'm struggling to see where that line comes from?
foreach ($response as $record):
Seems to a newbie that the $response is my original curl variable of which there is only one.
Mark Brady

yeah thats because you are using $insight instead of $response. Change line 17 to this

foreach ($response as $record) {
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mark Brady

sorry, my last comment should have read

foreach ($insight as $record) {

$response is a json string and the decoded json is now in $insight variable.

That should fix it for you
tjyoung

ASKER
Hi,
I tried it with insight as well but still get the same error message.
Tried a few extra variations as well but same error on foreach.
odd.
tjyoung

ASKER
Ray's produced the same result in my app as his example. Worked with no issues.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mark Brady

ok cool that sounds like the json decode did not work because if it is a valid json string it should have decoded into van associative array. In any case looks like rays example is what you need.
Ray Paseur

For better or worse, PHP uses different notation to deal with objects and associative arrays, and objects cannot have numerical indexes, but that said there is one big advantage to object notation.  The punctuation!  To get information from associative arrays, you need quotes around string literal keys, and that can add up to a lot of quotes.

$thing = $obj['person']['job']['title'];

In addition, even more punctuation is needed if you want to put that kind of notation into an echo statement.

echo "{$obj['person']['job']['title']}"; /* NOT EVEN SURE THIS WORKS! */

In contrast, you need only two characters to access data via PHP OOP notation, and no extra encapsulation for use in a quoted variable string.

$thing = $obj->person->job->title;
echo $obj->person->job->title;
echo "VOILA: $obj->person->job->title";

So I guess to sum it up, I've become a fan of the OOP notation and generally now lean away from the associative array whenever I can.
Mark Brady

Yeah I here you there Ray! I use only OOP these days but still I have to deal with code from others so I deal with a lot of assoc arrays. I'm used to it so it doesn't bother me but objects are tidier I guess.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

Yeah, I wish there were some kind of configuration setting that could return $_POST as an object.  PHP would run the class constructor on the raw request data.  Dream on...
Mark Brady

Haha yeah deam on alright! Hey why don't your write one for php and send it to them
Ray Paseur

@elvin66: It's not really that hard now that I look at it a little bit.  You get all of the raw request variables, so you can just instantiate a class that accesses these variables and maybe calls a filter method on each of the known request variables, then overwrites $_POST with the object.  I have a feeling this would confuse a lot of developers, tho...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mark Brady

Not a bad idea Ray. It would be even better if we could set a key in php.ini like request_type = object or request_type = array
So php would know how to build the $_POST and $_GET data. Maybe we should suggest that to them.