访问返回的数据

时间:2014-05-17 23:41:30

标签: php

我有一个php oauth请求,它向yahoo发送一个HTTP请求,我收到了一些私人用户信息。我可以打印出返回的内容,但如何选择单个变量?例如,我只想打印Jonathan Lucroy的名字。

以下是我用来打电话的内容:

** Heres a link to the oauth_client class for the referenced functions below **

    if(strlen($client->client_id) == 0
    || strlen($client->client_secret) == 0)
        die('Please go to Yahoo Apps page https://developer.apps.yahoo.com/projects/ , '.
            'create a project, and in the line '.$application_line.
            ' set the client_id to Consumer key and client_secret with Consumer secret. '.
            'The Callback URL must be '.$client->redirect_uri).' Make sure you enable the '.
            'necessary permissions to execute the API calls your application needs.';

    if(($success = $client->Initialize()))
    {
        if(($success = $client->Process()))
        {
            if(strlen($client->access_token))
            {
                $client->storeAccessToken($client->access_token);
                $success = $client->CallAPI(
                    'http://fantasysports.yahooapis.com/fantasy/v2/team/328.l.203329.t.12/roster;week=9', 
                    'GET', array(
                        'q'=>'',
                        'format'=>'json'
                    ), array('FailOnAccessError'=>true), $user);
                        echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
            }
        }
        $success = $client->Finalize($success);
    }
    if($client->exit)
        exit;
    if(strlen($client->authorization_error))
    {
        $client->error = $client->authorization_error;
        $success = false;
    }
    if($success)
    {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Yahoo OAuth client results</title>
</head>
<body>
<?php
        echo '<h1>', HtmlSpecialChars($user->query->results->profile->nickname), 
            ' you have logged in successfully with Yahoo!</h1>';
        echo '<pre>', HtmlSpecialChars(print_r($user, 1)), '</pre>';
        echo '<h6>'.$user.'</h6>';
?>
</body>
</html>

这就是我正在打印的内容:

stdClass Object
(
    [fantasy_content] => stdClass Object
        (
            [xml:lang] => en-US
            [yahoo:uri] => /fantasy/v2/team/328.l.203329.t.12/roster;week=9
            [team] => Array
                (
                    [0] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [team_key] => 328.l.203329.t.12
                                )

                            [1] => stdClass Object
                                (
                                    [team_id] => 12
                                )

                            [2] => stdClass Object
                                (
                                    [name] => Kevin Matesi's Team
                                )

                            [3] => stdClass Object
                                (
                                    [is_owned_by_current_login] => 1
                                )

                            [4] => stdClass Object
                                (
                                    [url] => http://baseball.fantasysports.yahoo.com/b1/203329/12
                                )

                            [5] => stdClass Object
                                (
                                    [team_logos] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [team_logo] => stdClass Object
                                                        (
                                                            [size] => large
                                                            [url] => http://l.yimg.com/dh/ap/fantasy/img/mlb/icon_1_lg.gif
                                                        )

                                                )

                                        )

                                )

                            [6] => Array
                                (
                                )

                            [7] => stdClass Object
                                (
                                    [waiver_priority] => 3
                                )

                            [8] => Array
                                (
                                )

                            [9] => stdClass Object
                                (
                                    [number_of_moves] => 0
                                )

                            [10] => stdClass Object
                                (
                                    [number_of_trades] => 0
                                )

                            [11] => stdClass Object
                                (
                                    [roster_adds] => stdClass Object
                                        (
                                            [coverage_type] => week
                                            [coverage_value] => 7
                                            [value] => 0
                                        )

                                )

                            [12] => Array
                                (
                                )

                            [13] => stdClass Object
                                (
                                    [managers] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [manager] => stdClass Object
                                                        (
                                                            [manager_id] => 12
                                                            [nickname] => Kmatesi
                                                            [guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
                                                            [is_current_login] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [roster] => stdClass Object
                                (
                                    [coverage_type] => date
                                    [date] => 2014-05-26
                                    [is_editable] => 1
                                    [0] => stdClass Object
                                        (
                                            [players] => stdClass Object
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [player] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [0] => stdClass Object
                                                                                (
                                                                                    [player_key] => 328.p.8609
                                                                                )

                                                                            [1] => stdClass Object
                                                                                (
                                                                                    [player_id] => 8609
                                                                                )

                                                                            [2] => stdClass Object
                                                                                (
                                                                                    [name] => stdClass Object
                                                                                        (
                                                                                            [full] => Jonathan Lucroy
                                                                                            [first] => Jonathan
                                                                                            [last] => Lucroy
                                                                                            [ascii_first] => Jonathan
                                                                                            [ascii_last] => Lucroy
                                                                                        )

                                                                                )

                                                                            [3] => stdClass Object
                                                                                (
                                                                                    [editorial_player_key] => mlb.p.8609
                                                                                )

                                                                            [4] => stdClass Object
                                                                                (
                                                                                    [editorial_team_key] => mlb.t.8
                                                                                )

                                                                            [5] => stdClass Object
                                                                                (
                                                                                    [editorial_team_full_name] => Milwaukee Brewers
                                                                                )

                                                                            [6] => stdClass Object
                                                                                (
                                                                                    [editorial_team_abbr] => Mil
                                                                                )

                                                                            [7] => stdClass Object
                                                                                (
                                                                                    [uniform_number] => 20
                                                                                )

                                                                            [8] => stdClass Object
                                                                                (
                                                                                    [display_position] => C,1B
                                                                                )

                                                                            [9] => stdClass Object
                                                                                (
                                                                                    [headshot] => stdClass Object
                                                                                        (
                                                                                            [url] => http://l.yimg.com/iu/api/res/1.2/LDlsOwlp8kcebcX0hWqbnw--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/mlb/players_l/20130405/8609.1.jpg
                                                                                            [size] => small
                                                                                        )

                                                                                    [image_url] => http://l.yimg.com/iu/api/res/1.2/LDlsOwlp8kcebcX0hWqbnw--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/mlb/players_l/20130405/8609.1.jpg
                                                                                )

                                                                            [10] => stdClass Object
                                                                                (
                                                                                    [is_undroppable] => 0
                                                                                )

                                                                            [11] => stdClass Object
                                                                                (
                                                                                    [position_type] => B
                                                                                )

                                                                            [12] => stdClass Object
                                                                                (
                                                                                    [eligible_positions] => Array
                                                                                        (
                                                                                            [0] => stdClass Object
                                                                                                (
                                                                                                    [position] => C
                                                                                                )

                                                                                            [1] => stdClass Object
                                                                                                (
                                                                                                    [position] => 1B
                                                                                                )

                                                                                            [2] => stdClass Object
                                                                                                (
                                                                                                    [position] => Util
                                                                                                )

                                                                                        )

                                                                                )

                                                                            [13] => stdClass Object
                                                                                (
                                                                                    [has_player_notes] => 1
                                                                                )

                                                                            [14] => stdClass Object
                                                                                (
                                                                                    [has_recent_player_notes] => 1
                                                                                )

                                                                        )

                                                                    [1] => stdClass Object

1 个答案:

答案 0 :(得分:0)

Regarding this line:

     if(($success = $client->Process()))

This is not a correct way to write the IF statement
The problem is an assignment always returns 'true'

A much better technique is: ( use a comparison, similar to the following)

 if( false != ($success = $client->Process()) )

BTW: never compare to 'true' because 'true' can be/is defined as 'not false'
where false is 0, so any non 0 value will be 'not false'
where a compare to 'true' is looking for a specific value to match.