开发者

Extract Data from a Website using PHP

I am trying to get PHP to extract the TOKEN (the uppercase one), USERID (uppercase), and the USER NAME (uppercase) from a web page with the following text.

{
   "rsp":{
      "stat":"ok",
      "auth":{
         "token":"**TOKEN**",
         "perms":"read",
         "user":{
            "id":"**USERID**",
            "username":"**USER NAME**",
            "fullname":"**NAME OF USER**"
         }
      }
   }
}

(This is from the RTM api, getting the authentication token of the user).

How would I go about doing this? Thanks!

EDIT:

how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!

{
   "rsp":{
      "stat":"ok",
      "tasks":{
         "rev":"REV_NUMBER",
         "list":{
            "id":"ID_NUMBER",
            "taskse­­ries":{
               "id":"ID_NUMBER",
               "created":"2010-11-16T00:01:50Z",
               "modified":"2011-02-28T05:09:36Z",
               "name":"Buy Milk!",
               "source":"js",
               "url":"",
               "location_id":"",
               "rrule":{
                  "every":"1",
                  "$t":"FREQ=W­­EEKLY;INTERVAL=1"
               },
               "tags":[

               ],
               "participants":[

               ],
               "notes":[

               ],
               "task":{
                  "id":"ID_NUMBER"                  ­­,
                  "due":"2011-02-28T05:00:00Z",
                  "has_due_time":"0",
                  "added":"2011-02-21T05:04:49Z",
                  "completed":""开发者_StackOverflow社区,
                  "deleted":"",
                  "priority":"2",
                  "postponed":"0",
                  "estima­­te":""
               }
            }
         }
      }
   }
}


As Delan suggested, use json_decode. Here is an example of how you would use json_decode to extract the information you require.

// your json string
$string = '{"rsp":{"stat":"ok","auth":{"token":"**TOKEN**","perms":"read","user":{"id":"**USERID**","username":"**USER NAME**","fullname":"**NAME OF USER**"}}}}';

// parse json string to an array
$array = json_decode($string, true);

// auth token
echo $array['rsp']['auth']['token'];

// user details
echo $array['rsp']['auth']['user']['id'];
echo $array['rsp']['auth']['user']['username'];
echo $array['rsp']['auth']['user']['fullname'];

UPDATE I've updated the code to use json_decode's $assoc parameter to convert from an object to an assoc array.

ANOTHER UPDATE To answer your updated question..

how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!

This code would work to get the values that you want.

//string(9) "Buy Milk!"
echo $array['rsp']['tasks']['list']['taskseries']['name'];

// string(20) "2011-02-28T05:00:00Z"
echo $array['rsp']['tasks']['list']['taskseries']['task']['due'];

This code isn't ideal, but it gets the job done for you.


If the string is JSON, json_decode in PHP will do the trick. It's was implemented in PHP 5.2.0.

http://www.php.net/manual/en/function.json-decode.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜