Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm a noob at APIs and JSON that would appreciate any help on the following. I'm using the PHP Linkedin Library to run People Queries.

Here's the relevant code:

<?php
            $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
            $keywords = (isset($_GET['keywords'])) ? $_GET['keywords'] : "Marketing";
            ?>
            <form action="<?php echo $_SERVER['PHP_SELF'];?>#peopleSearch" method="get">
                Search by Keywords: <input type="text" value="<?php echo $keywords?>" name="keywords" /><input type="submit" value="Search" />
            </form>
            <?php 
            $query    = '?sort=distance&current-company=true&keywords='.$keywords;
            $response = $OBJ_linkedin->searchPeople($query);

            if($response['success'] === TRUE) {


 echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>";
            } else {
              // request failed
              echo "Error retrieving people search results:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";                
            }
          } else {
            // user isn't connected
            ?>

Here's an excerpt of the output I get

{"people": {
  "_count": 10,
  "_start": 0,
  "_total": 11,
  "values": [
    {
      "firstName": "Peter",
      "headline": "Frontend Engineer at Lot18",
      "id": "kYZ3B2hHYH",
      "lastName": "Welch",
      "pictureUrl": "http://m.c.lnkd.licdn.com/mpr/mprx/0_e0hbvSXvhiSoTO2PERiqvfLV850d342PoOq4vakwx8IJOyR1XJrwRmr5mIx9C0DxWpGMsWVjBZEQ",
      "relationToViewer": {"distance": 3}
    },

  ]
}}

I'd like to capture the fields like "firstname" and "pictureUrl" into variables that I can use elsewhere. E.g.

<img src="<?php echo $picture-url; ?>" />

How would I go about doing that? I've spent days searching/trying to figure this out and still no luck. Any help is much appreciated!

share|improve this question
add comment

1 Answer

The response format is a JSON object that is encoded as a string

$response['linkedin']

To get back the object, use:

$responseObject = json_decode($response['linkedin']);

Then you can access variables like this

$responseObject->values[0]->pictureUrl
share|improve this answer
 
I placed that code in the "if" statement above but I'm not getting any results. Here's the pastebin if($response['success'] === TRUE) { // echo "<pre>" . print_r($response['linkedin'], TRUE) . "</pre>"; $responseObject = json_decode($response['linkedin']); echo $responseObject->values[0]->pictureUrl; } –  ace973 Oct 7 '13 at 23:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.