Пример #1
0
 /**
  * Register default mime handlers.  Is idempotent.
  */
 public static function registerHandlers()
 {
     if (self::$registered === true) {
         return;
     }
     // @todo check a conf file to load from that instead of
     // hardcoding into the library?
     $handlers = array(\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(), \Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(), \Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(), \Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler());
     foreach ($handlers as $mime => $handler) {
         Httpful::register($mime, $handler);
     }
     self::$registered = true;
 }
Пример #2
0
 /**
  * Register default mime handlers.  Is idempotent.
  */
 public static function registerHandlers()
 {
     if (self::$registered === true) {
         return;
     }
     // @todo check a conf file to load from that instead of
     // hardcoding into the library?
     $handlers = array(Mime::JSON => new Handlers\JsonHandler(), Mime::XML => new Handlers\XmlHandler(), Mime::FORM => new Handlers\FormHandler(), Mime::CSV => new Handlers\CsvHandler());
     foreach ($handlers as $mime => $handler) {
         // Don't overwrite if the handler has already been registered
         if (Httpful::hasParserRegistered($mime)) {
             continue;
         }
         Httpful::register($mime, $handler);
     }
     self::$registered = true;
 }
Пример #3
0
 /**
  * Parse the response into a clean data structure
  * (most often an associative array) based on the expected
  * Mime type.
  * @return array|string|object the response parse accordingly
  * @param string Http response body
  */
 public function _parse($body)
 {
     // If the user decided to forgo the automatic
     // smart parsing, short circuit.
     if (!$this->request->auto_parse) {
         return $body;
     }
     // If provided, use custom parsing callback
     if (isset($this->request->parse_callback)) {
         return call_user_func($this->request->parse_callback, $body);
     }
     // Decide how to parse the body of the response in the following order
     //  1. If provided, use the mime type specifically set as part of the `Request`
     //  2. If a MimeHandler is registered for the content type, use it
     //  3. If provided, use the "parent type" of the mime type from the response
     //  4. Default to the content-type provided in the response
     $parse_with = $this->request->expected_type;
     if (empty($this->request->expected_type)) {
         $parse_with = Httpful::hasParserRegistered($this->content_type) ? $this->content_type : $this->parent_type;
     }
     return Httpful::get($parse_with)->parse($body);
 }
 private function getLondonData($stop)
 {
     $baseUrl = Config::get('traveline.tflurl');
     $urlparameters = '?StopCode2=' . $stop . '&ReturnList=LineName,DestinationText,EstimatedTime';
     $response = Httpful::get($baseUrl . $urlparameters)->parsewith(function ($body) {
         $messages = explode("\n", $body);
         foreach ($messages as $message) {
             $message = json_decode($message, TRUE);
             if ($message[0] === 1) {
                 //Hacky as shit
                 $stoppingtimes[$message[3]] = ['BusName' => $message[1], 'BusHeading' => $message[2], 'ArrivalTime' => $message[3] / 1000];
             }
         }
         if (!empty($stoppingtimes)) {
             ksort($stoppingtimes);
             return $stoppingtimes;
         } else {
             return FALSE;
         }
     })->send();
     return $response->body;
 }
Пример #5
0
 /**
  * Turn payload from structured data into
  * a string based on the current Mime type.
  * This uses the auto_serialize option to determine
  * it's course of action.  See serialize method for more.
  * Renamed from _detectPayload to _serializePayload as of
  * 2012-02-15.
  *
  * Added in support for custom payload serializers.
  * The serialize_payload_method stuff still holds true though.
  * @see Request::registerPayloadSerializer()
  *
  * @return string
  * @param mixed $payload
  */
 private function _serializePayload($payload)
 {
     if (empty($payload) || $this->serialize_payload_method === self::SERIALIZE_PAYLOAD_NEVER) {
         return $payload;
     }
     // When we are in "smart" mode, don't serialize strings/scalars, assume they are already serialized
     if ($this->serialize_payload_method === self::SERIALIZE_PAYLOAD_SMART && is_scalar($payload)) {
         return $payload;
     }
     // Use a custom serializer if one is registered for this mime type
     if (isset($this->payload_serializers['*']) || isset($this->payload_serializers[$this->content_type])) {
         $key = isset($this->payload_serializers[$this->content_type]) ? $this->content_type : '*';
         return call_user_func($this->payload_serializers[$key], $payload);
     }
     return Httpful::get($this->content_type)->serialize($payload);
 }