ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/facebook/trunk/facebookapi_php5_restlib.php
(Generate patch)

Comparing facebook/trunk/facebookapi_php5_restlib.php (file contents):
Revision 943 by douglas, 2007-08-29T17:44:10-07:00 vs.
Revision 950 by douglas, 2007-09-29T14:02:03-07:00

# Line 1 | Line 1
1   <?php
2 + # vim: expandtab shiftwidth=2 tabstop=2
3 + #
4 + # Modified to use JSON when $json is set to true.
5 + #
6 + # Douglas Thrift
7 + #
8 + # $Id$
9   //
10   // +---------------------------------------------------------------------------+
11 < // | Facebook Platform PHP5 client                                 |
11 > // | Facebook Platform PHP5 client                                             |
12   // +---------------------------------------------------------------------------+
13 < // | Copyright (c) 2007 Facebook, Inc.                                         |
13 > // | Copyright (c) 2007 Facebook, Inc.                                         |
14   // | All rights reserved.                                                      |
15   // |                                                                           |
16   // | Redistribution and use in source and binary forms, with or without        |
# Line 37 | Line 44 | class FacebookRestClient {
44    public $api_key;
45    public $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages
46    public $added;        // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages
47 +  public $json;
48  
49    /**
50     * Create the client.
# Line 140 | Line 148 | function toggleDisplay(id, type) {
148                                            $image_1=null, $image_1_link=null,
149                                            $image_2=null, $image_2_link=null,
150                                            $image_3=null, $image_3_link=null,
151 <                                          $image_4=null, $image_4_link=null,
144 <                                          $priority=1) {
151 >                                          $image_4=null, $image_4_link=null) {
152      return $this->call_method('facebook.feed.publishStoryToUser',
153        array('title' => $title,
154              'body' => $body,
# Line 152 | Line 159 | function toggleDisplay(id, type) {
159              'image_3' => $image_3,
160              'image_3_link' => $image_3_link,
161              'image_4' => $image_4,
162 <            'image_4_link' => $image_4_link,
156 <            'priority' => $priority));
162 >            'image_4_link' => $image_4_link));
163    }
164                                            
165    public function feed_publishActionOfUser($title, $body,
166                                             $image_1=null, $image_1_link=null,
167                                             $image_2=null, $image_2_link=null,
168                                             $image_3=null, $image_3_link=null,
169 <                                           $image_4=null, $image_4_link=null,
164 <                                           $priority=1) {
169 >                                           $image_4=null, $image_4_link=null) {
170      return $this->call_method('facebook.feed.publishActionOfUser',
171        array('title' => $title,
172              'body' => $body,
# Line 172 | Line 177 | function toggleDisplay(id, type) {
177              'image_3' => $image_3,
178              'image_3_link' => $image_3_link,
179              'image_4' => $image_4,
180 <            'image_4_link' => $image_4_link,
176 <            'priority' => $priority));
180 >            'image_4_link' => $image_4_link));
181    }
182  
183    /**
# Line 372 | Line 376 | function toggleDisplay(id, type) {
376    /* UTILITY FUNCTIONS */
377  
378    public function call_method($method, $params) {
379 <    $xml = $this->post_request($method, $params);
380 <    $sxml = simplexml_load_string($xml);
381 <    $result = self::convert_simplexml_to_array($sxml);
382 <    if ($GLOBALS['facebook_config']['debug']) {
383 <      // output the raw xml and its corresponding php object, for debugging:
384 <      print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">';
385 <      $this->cur_id++;
386 <      print $this->cur_id . ': Called ' . $method . ', show ' .
387 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '.
388 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '.
389 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '.
390 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>';
391 <      print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>';
392 <      print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>';
393 <      print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>';
394 <      print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>';
395 <      print '</div>';
379 >    if ($this->json) {
380 >      $json = $this->post_request($method, $params);
381 >      # XXX: silly facebook with its invalid JSON
382 >      $valid = preg_match('/^[\[{].*[\]}]$/', $json);
383 >      $array = json_decode($valid ? $json : "[$json]", true);
384 >      $result = $valid ? $array : $array[0];
385 >    } else {
386 >      $xml = $this->post_request($method, $params);
387 >      $sxml = simplexml_load_string($xml);
388 >      $result = self::convert_simplexml_to_array($sxml);
389 >      if ($GLOBALS['facebook_config']['debug']) {
390 >        // output the raw xml and its corresponding php object, for debugging:
391 >        print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">';
392 >        $this->cur_id++;
393 >        print $this->cur_id . ': Called ' . $method . ', show ' .
394 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '.
395 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '.
396 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '.
397 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>';
398 >        print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>';
399 >        print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>';
400 >        print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>';
401 >        print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>';
402 >        print '</div>';
403 >      }
404      }
405      if (is_array($result) && isset($result['error_code'])) {
406        throw new FacebookRestClientException($result['error_msg'], $result['error_code']);
# Line 408 | Line 420 | function toggleDisplay(id, type) {
420      if (!isset($params['v'])) {
421        $params['v'] = '1.0';
422      }
423 +    if ($this->json)
424 +      $params['format'] = 'JSON';
425      $post_params = array();
426      foreach ($params as $key => &$val) {
427        if (is_array($val)) $val = implode(',', $val);
428        $post_params[] = $key.'='.urlencode($val);
429      }
430 <    $secret = $this->secret;
417 <    $post_params[] = 'sig='.Facebook::generate_sig($params, $secret);
430 >    $post_params[] = 'sig='.Facebook::generate_sig($params, $this->secret);
431      $post_string = implode('&', $post_params);
432  
433 <    if (function_exists('curl_init')) {
433 >    if ($this->json) {
434 >      $result = file_get_contents($this->server_addr, false, stream_context_create(
435 >        array('http' =>
436 >              array('method' => 'POST',
437 >                    'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
438 >                                'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n".
439 >                                'Content-length: ' . strlen($post_string),
440 >                    'content' => $post_string))));
441 >    } elseif (function_exists('curl_init')) {
442        // Use CURL if installed...
443        $ch = curl_init();
444        curl_setopt($ch, CURLOPT_URL, $this->server_addr);

Comparing facebook/trunk/facebookapi_php5_restlib.php (property svn:executable):
Revision 943 by douglas, 2007-08-29T17:44:10-07:00 vs.
Revision 950 by douglas, 2007-09-29T14:02:03-07:00

# Line 1 | Line 0
1 *

Comparing facebook/trunk/facebookapi_php5_restlib.php (property svn:keywords):
Revision 943 by douglas, 2007-08-29T17:44:10-07:00 vs.
Revision 950 by douglas, 2007-09-29T14:02:03-07:00

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines