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 945 by douglas, 2007-08-29T17:54:17-07:00 vs.
Revision 951 by douglas, 2007-10-10T16:50:56-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                                             |
# 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,
181 <            'priority' => $priority));
180 >            'image_4_link' => $image_4_link));
181 >  }
182 >
183 >  public function feed_publishTemplatizedAction($actor_id, $title_template, $title_data,
184 >                                                $body_template, $body_data, $body_general,
185 >                                                $image_1=null, $image_1_link=null,
186 >                                                $image_2=null, $image_2_link=null,
187 >                                                $image_3=null, $image_3_link=null,
188 >                                                $image_4=null, $image_4_link=null,
189 >                                                $target_ids=null) {
190 >    return $this->call_method('facebook.feed.publishTemplatizedAction',
191 >      array('actor_id' => $actor_id,
192 >            'title_template' => $title_template,
193 >            'title_data' => $title_data,
194 >            'body_template' => $body_template,
195 >            'body_data' => $body_data,
196 >            'body_general' => $body_general,
197 >            'image_1' => $image_1,
198 >            'image_1_link' => $image_1_link,
199 >            'image_2' => $image_2,
200 >            'image_2_link' => $image_2_link,
201 >            'image_3' => $image_3,
202 >            'image_3_link' => $image_3_link,
203 >            'image_4' => $image_4,
204 >            'image_4_link' => $image_4_link
205 >            'target_ids' => $target_ids));
206    }
207  
208    /**
# Line 372 | Line 401 | function toggleDisplay(id, type) {
401    /* UTILITY FUNCTIONS */
402  
403    public function call_method($method, $params) {
404 <    $xml = $this->post_request($method, $params);
405 <    $sxml = simplexml_load_string($xml);
406 <    $result = self::convert_simplexml_to_array($sxml);
407 <    if ($GLOBALS['facebook_config']['debug']) {
408 <      // output the raw xml and its corresponding php object, for debugging:
409 <      print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">';
410 <      $this->cur_id++;
411 <      print $this->cur_id . ': Called ' . $method . ', show ' .
412 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '.
413 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '.
414 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '.
415 <            '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>';
416 <      print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>';
417 <      print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>';
418 <      print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>';
419 <      print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>';
420 <      print '</div>';
404 >    if ($this->json) {
405 >      $json = $this->post_request($method, $params);
406 >      # XXX: silly facebook with its invalid JSON
407 >      $valid = preg_match('/^[\[{].*[\]}]$/', $json);
408 >      $array = json_decode($valid ? $json : "[$json]", true);
409 >      $result = $valid ? $array : $array[0];
410 >    } else {
411 >      $xml = $this->post_request($method, $params);
412 >      $sxml = simplexml_load_string($xml);
413 >      $result = self::convert_simplexml_to_array($sxml);
414 >      if ($GLOBALS['facebook_config']['debug']) {
415 >        // output the raw xml and its corresponding php object, for debugging:
416 >        print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">';
417 >        $this->cur_id++;
418 >        print $this->cur_id . ': Called ' . $method . ', show ' .
419 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '.
420 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '.
421 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '.
422 >              '<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>';
423 >        print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>';
424 >        print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>';
425 >        print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>';
426 >        print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>';
427 >        print '</div>';
428 >      }
429      }
430      if (is_array($result) && isset($result['error_code'])) {
431        throw new FacebookRestClientException($result['error_msg'], $result['error_code']);
# Line 408 | Line 445 | function toggleDisplay(id, type) {
445      if (!isset($params['v'])) {
446        $params['v'] = '1.0';
447      }
448 +    if ($this->json)
449 +      $params['format'] = 'JSON';
450      $post_params = array();
451      foreach ($params as $key => &$val) {
452        if (is_array($val)) $val = implode(',', $val);
453        $post_params[] = $key.'='.urlencode($val);
454      }
455 <    $secret = $this->secret;
417 <    $post_params[] = 'sig='.Facebook::generate_sig($params, $secret);
455 >    $post_params[] = 'sig='.Facebook::generate_sig($params, $this->secret);
456      $post_string = implode('&', $post_params);
457  
458 <    if (function_exists('curl_init')) {
458 >    if ($this->json) {
459 >      $result = file_get_contents($this->server_addr, false, stream_context_create(
460 >        array('http' =>
461 >              array('method' => 'POST',
462 >                    'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
463 >                                'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n".
464 >                                'Content-length: ' . strlen($post_string),
465 >                    'content' => $post_string))));
466 >    } elseif (function_exists('curl_init')) {
467        // Use CURL if installed...
468        $ch = curl_init();
469        curl_setopt($ch, CURLOPT_URL, $this->server_addr);

Comparing facebook/trunk/facebookapi_php5_restlib.php (property svn:keywords):
Revision 945 by douglas, 2007-08-29T17:54:17-07:00 vs.
Revision 951 by douglas, 2007-10-10T16:50:56-07:00

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines