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 | |
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. |
380 |
|
/* UTILITY FUNCTIONS */ |
381 |
|
|
382 |
|
public function call_method($method, $params) { |
383 |
< |
$xml = $this->post_request($method, $params); |
384 |
< |
$sxml = simplexml_load_string($xml); |
385 |
< |
$result = self::convert_simplexml_to_array($sxml); |
386 |
< |
if ($GLOBALS['facebook_config']['debug']) { |
387 |
< |
// output the raw xml and its corresponding php object, for debugging: |
388 |
< |
print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">'; |
389 |
< |
$this->cur_id++; |
390 |
< |
print $this->cur_id . ': Called ' . $method . ', show ' . |
391 |
< |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '. |
392 |
< |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '. |
393 |
< |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '. |
394 |
< |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>'; |
395 |
< |
print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>'; |
396 |
< |
print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>'; |
397 |
< |
print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>'; |
398 |
< |
print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>'; |
399 |
< |
print '</div>'; |
383 |
> |
if ($this->json) { |
384 |
> |
$json = $this->post_request($method, $params); |
385 |
> |
# XXX: silly facebook with its invalid JSON |
386 |
> |
$valid = preg_match('/^[\[{].*[\]}]$/', $json); |
387 |
> |
$array = json_decode($valid ? $json : "[$json]", true); |
388 |
> |
$result = $valid ? $array : $array[0]; |
389 |
> |
} else { |
390 |
> |
$xml = $this->post_request($method, $params); |
391 |
> |
$sxml = simplexml_load_string($xml); |
392 |
> |
$result = self::convert_simplexml_to_array($sxml); |
393 |
> |
if ($GLOBALS['facebook_config']['debug']) { |
394 |
> |
// output the raw xml and its corresponding php object, for debugging: |
395 |
> |
print '<div style="margin: 10px 30px; padding: 5px; border: 2px solid black; background: gray; color: white; font-size: 12px; font-weight: bold;">'; |
396 |
> |
$this->cur_id++; |
397 |
> |
print $this->cur_id . ': Called ' . $method . ', show ' . |
398 |
> |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'params\');">Params</a> | '. |
399 |
> |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'xml\');">XML</a> | '. |
400 |
> |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'sxml\');">SXML</a> | '. |
401 |
> |
'<a href=# onclick="return toggleDisplay(' . $this->cur_id . ', \'php\');">PHP</a>'; |
402 |
> |
print '<pre id="params'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($params, true).'</pre>'; |
403 |
> |
print '<pre id="xml'.$this->cur_id.'" style="display: none; overflow: auto;">'.htmlspecialchars($xml).'</pre>'; |
404 |
> |
print '<pre id="php'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($result, true).'</pre>'; |
405 |
> |
print '<pre id="sxml'.$this->cur_id.'" style="display: none; overflow: auto;">'.print_r($sxml, true).'</pre>'; |
406 |
> |
print '</div>'; |
407 |
> |
} |
408 |
|
} |
409 |
|
if (is_array($result) && isset($result['error_code'])) { |
410 |
|
throw new FacebookRestClientException($result['error_msg'], $result['error_code']); |
424 |
|
if (!isset($params['v'])) { |
425 |
|
$params['v'] = '1.0'; |
426 |
|
} |
427 |
+ |
if ($this->json) |
428 |
+ |
$params['format'] = 'JSON'; |
429 |
|
$post_params = array(); |
430 |
|
foreach ($params as $key => &$val) { |
431 |
|
if (is_array($val)) $val = implode(',', $val); |
432 |
|
$post_params[] = $key.'='.urlencode($val); |
433 |
|
} |
434 |
< |
$secret = $this->secret; |
417 |
< |
$post_params[] = 'sig='.Facebook::generate_sig($params, $secret); |
434 |
> |
$post_params[] = 'sig='.Facebook::generate_sig($params, $this->secret); |
435 |
|
$post_string = implode('&', $post_params); |
436 |
|
|
437 |
< |
if (function_exists('curl_init')) { |
437 |
> |
if ($this->json) { |
438 |
> |
$result = file_get_contents($this->server_addr, false, stream_context_create( |
439 |
> |
array('http' => |
440 |
> |
array('method' => 'POST', |
441 |
> |
'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n". |
442 |
> |
'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) '.phpversion()."\r\n". |
443 |
> |
'Content-length: ' . strlen($post_string), |
444 |
> |
'content' => $post_string)))); |
445 |
> |
} elseif (function_exists('curl_init')) { |
446 |
|
// Use CURL if installed... |
447 |
|
$ch = curl_init(); |
448 |
|
curl_setopt($ch, CURLOPT_URL, $this->server_addr); |