Exemple #1
0
/**
 * Takes care of JSON rendering for the REST-based API. We do this ourselves
 * so that we can properly handle our special quirks like invisibility.
 *
 * @param  $object object to be serialized by json encoding
 * @return string representing serialized form of $object
 */
function api_json2_render_object($object)
{
    $json = '';
    if ($object instanceof FQLCantSee) {
        $json .= 'null';
    } else {
        if (is_array($object)) {
            $list = key($object) === 0;
            $json .= $list ? '[' : '{';
            if (!empty($object)) {
                $values = array();
                foreach ($object as $k => $v) {
                    $val = '';
                    if (!$list) {
                        $val .= json_encode($k) . ':';
                    }
                    $val .= api_json2_render_object($v);
                    $values[] = $val;
                }
                $json .= implode(',', $values);
            }
            $json .= $list ? ']' : '}';
        } else {
            if (is_object($object)) {
                $json .= '{';
                $values = array();
                foreach ($object as $k => $v) {
                    if (isset($v)) {
                        $values[] = json_encode($k) . ':' . api_json2_render_object($v);
                    }
                }
                $json .= implode(',', $values);
                $json .= '}';
            } else {
                $json .= json_encode($object);
            }
        }
    }
    return $json;
}
 private function process_request($request)
 {
     global $API_DOMAIN, $API_DOMAIN_DOT_SUFFIX;
     $app_id = $this->app_id;
     $method = $request['method'];
     $callback = false;
     $serialized_result = '';
     // Initialize result
     $result = array();
     // Fix method name
     if (starts_with($method, $API_DOMAIN . '.')) {
         $method = substr($method, 9);
     }
     // Replace periods with underscores in method name
     $method_underscore = str_replace('.', '_', $method);
     $ec = $this->check_throttle($method_underscore, $request);
     if ($ec !== API_EC_SUCCESS) {
         $msg = $api_error_descriptions[$ec];
         if ($ec === API_EC_BAD_IP) {
             $msg .= ' (ip was: ' . $_SERVER['REMOTE_ADDR'] . ')';
         }
         throw new api10_FacebookApiException(array('error_code' => $ec, 'error_msg' => $msg));
     }
     $impl = new FacebookApi10Implementation($app_id, $this->user_id, $this->session_key, $this->format);
     $api = new FacebookApi10Rest($impl);
     // Check that the method is valid
     if (!method_exists($api, $method_underscore) || !method_exists($impl, $method_underscore) || !api_can_call_method($app_id, $method_underscore)) {
         $ec = api10_FacebookApiErrorCode::API_EC_METHOD;
         throw new api10_FacebookApiException(array('error_code' => $ec, 'error_msg' => $GLOBALS['api_error_descriptions'][$ec]));
     } else {
         // Call the method and catch any exceptions
         $result = $api->{$method_underscore}($request);
     }
     switch ($this->format) {
         case 'manual':
             print api_xml_render_manual_error($ec, $msg, $request);
             break;
         case 'xml':
             // Prepare the XML response
             $xml_memory = xmlwriter_open_memory();
             xmlwriter_set_indent($xml_memory, true);
             xmlwriter_set_indent_string($xml_memory, '  ');
             xmlwriter_start_document($xml_memory, API_VERSION_10, 'UTF-8');
             if ($result instanceof Exception) {
                 $name = 'error_response';
             } else {
                 $name = $method_underscore . '_response';
             }
             $attrs = array();
             // FBOPEN:NOTE here, if you are not publishing your own .xsd, to use 'facebook.com' instead
             // of $API_DOMAIN_DOT_SUFFIX
             $attrs['xmlns'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/';
             $attrs['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
             if ($method_underscore != 'fql_query') {
                 $attrs['xsi:schemaLocation'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/ http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . API_VERSION_10 . '/facebook.xsd';
             }
             if (is_array($result) && isset($result[0]) && $result[0] instanceof xml_element) {
                 $attrs['list'] = 'true';
                 api_xml3_render_object($xml_memory, new xml_element($name, $result, $attrs));
             } else {
                 api_xml2_render_object($xml_memory, $name, $result, $attrs);
             }
             xmlwriter_end_document($xml_memory);
             // Write XML response
             $xml = xmlwriter_output_memory($xml_memory, true);
             if ($callback) {
                 $xml = addslashes($xml);
                 $xml = str_replace("\n", '\\n', $xml);
                 $serialized_result = $callback . '(\'' . $xml . '\');';
             } else {
                 $serialized_result = $xml;
             }
             break;
         case 'json':
             $json = api_json2_render_object($result);
             if ($callback) {
                 $serialized_result = $callback . '(' . $json . ');';
             } else {
                 $serialized_result = $json;
             }
             break;
     }
     return $serialized_result;
 }
Exemple #3
0
                if ($method_underscore != 'fql_query') {
                    $attrs['xsi:schemaLocation'] = 'http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . $version . '/ http://api.' . $API_DOMAIN_DOT_SUFFIX . '/' . $version . '/facebook.xsd';
                }
                if (is_array($result) && isset($result[0]) && $result[0] instanceof xml_element) {
                    $attrs['list'] = 'true';
                    api_xml3_render_object($xml_memory, new xml_element($name, $result, $attrs));
                } else {
                    api_xml2_render_object($xml_memory, $name, $result, $attrs);
                }
                break;
        }
        xmlwriter_end_document($xml_memory);
        // Write XML response
        $xml = xmlwriter_output_memory($xml_memory, true);
        if ($req_callback) {
            $xml = addslashes($xml);
            $xml = str_replace("\n", '\\n', $xml);
            echo $req_callback . '(\'' . $xml . '\');';
        } else {
            echo $xml;
        }
        break;
    case 'json':
        $json = api_json2_render_object($result);
        if ($req_callback) {
            echo $req_callback . '(' . $json . ');';
        } else {
            echo $json;
        }
        break;
}