Пример #1
1
 /**
  * Construt a Request
  * 
  * @param Auth $auth SkyHub Auth information to send on headers
  */
 public function __construct(Auth $auth)
 {
     $this->auth = $auth;
     $this->jsonHandler = new \SkyHub\Handlers\JsonHandler();
     $this->jsonHandler->init(array());
     Httpful::register(\Httpful\Mime::JSON, $this->jsonHandler);
     // creates a request template, every request must have the auth headers
     $this->requestTemplate = HttpfulRequest::init()->expectsJson()->addHeader('X-User-Email', $this->auth->getEmail())->addHeader('X-User-Token', $this->auth->getToken());
     HttpfulRequest::ini($this->requestTemplate);
 }
Пример #2
0
 /**
  * @param array $conf
  *    host: string required ip or hostname
  *    port: int required
  *    secure: bool optional use https?
  */
 public function init(array $conf)
 {
     $secure = empty($conf['secure']) ? '' : 's';
     $this->evaluator_uri = sprintf('http%s://%s:%s/', $secure, $conf['evaluator']['host'], $conf['evaluator']['port']);
     $this->collector_uri = sprintf('http%s://%s:%s/', $secure, $conf['collector']['host'], $conf['collector']['port']);
     // Needed to tell httpful to use arrays instead of plain objects for json responses
     \Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));
 }
 /**
  * Bootstrap the service provider
  * 
  * @return void
  */
 public function boot()
 {
     // Publish a demo service description
     $this->publishes([__DIR__ . '/../config/httpbin.php' => config_path('webservice/httpbin.php')]);
     // Register our custom parsers with Httpful
     Httpful::register(HttpfulMime::XML, new CustomXmlHandler());
     Httpful::register(HttpfulMime::JSON, new CustomJsonHandler());
 }
Пример #4
0
 public function __construct()
 {
     $this->setSessionId();
     $this->root = $this->getRoot();
     $this->tattlerApi = (config()->get('grohman.tattler::ssl') == true ? 'https' : 'http') . '://' . $this->getTattlerUri();
     $json_handler = new JsonHandler(['decode_as_array' => true]);
     Httpful::register('application/json', $json_handler);
     $this->restful = HRequest::init();
     $this->restful->uri($this->tattlerApi);
 }
Пример #5
0
{
    /**
     * Takes a response body, and turns it into
     * a two dimensional array.
     *
     * @param string $body
     * @return mixed
     */
    public function parse($body)
    {
        return str_getcsv($body);
    }
    /**
     * Takes a two dimensional array and turns it
     * into a serialized string to include as the
     * body of a request
     *
     * @param mixed $payload
     * @return string
     */
    public function serialize($payload)
    {
        $serialized = '';
        foreach ($payload as $line) {
            $serialized .= '"' . implode('","', $line) . '"' . "\n";
        }
        return $serialized;
    }
}
\Httpful\Httpful::register('text/csv', new SimpleCsvHandler());
Пример #6
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);
 }
Пример #7
0
 public function testOverrideXmlHandler()
 {
     // Lazy test...
     $prev = \Httpful\Httpful::get(\Httpful\Mime::XML);
     $this->assertEquals($prev, new \Httpful\Handlers\XmlHandler());
     $conf = array('namespace' => 'http://example.com');
     \Httpful\Httpful::register(\Httpful\Mime::XML, new \Httpful\Handlers\XmlHandler($conf));
     $new = \Httpful\Httpful::get(\Httpful\Mime::XML);
     $this->assertNotEquals($prev, $new);
 }
Пример #8
0
<?php

require __DIR__ . '/../bootstrap.php';
use Httpful\Request;
// Get event details for a public event
$uri = "http://api.showclix.com/Event/8175";
$response = Request::get($uri)->expectsType('json')->send();
// Print out the event details
echo "The event {$response->body->event} will take place on {$response->body->event_start}\n";
// Example overriding the default JSON handler with one that encodes the response as an array
\Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));
$response = Request::get($uri)->expectsType('json')->send();
// Print out the event details
echo "The event {$response->body['event']} will take place on {$response->body['event_start']}\n";
Пример #9
0
 public static function postFormJson($url, $data)
 {
     \Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));
     $response = \Httpful\Request::post($url, $data, \Httpful\Mime::FORM)->expectsJson()->send();
     return $response->body;
 }
Пример #10
0
 /**
  * Parse the response into a clean data structure
  * (most often an associative array) based on the expected
  * Mime type.
  *
  * @param string $body Http response body
  *
  * @return mixed the response parse accordingly
  */
 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);
 }
Пример #11
0
 /**
  * 将json对象解析结构改为数组
  */
 protected static function setJsonDecodeAsArray()
 {
     Httpful::get(Mime::JSON)->init(array('decode_as_array', true));
 }
 /**
  * Method to set template of request
  */
 private function setTemplate($method)
 {
     // Register a new mime type handler if we expect a Json, so the response is decoded as an array.
     // Extracted from: http://stackoverflow.com/a/22597037
     if ($this->mimeType == 'json') {
         $jsonHandler = new JsonHandler(array('decode_as_array' => true));
         Httpful::register('application/json', $jsonHandler);
     }
     // Create template
     $template = Request::init()->method($this->getMethod($method))->expects($this->mimeType)->sendsType(Mime::FORM);
     // Add custom headers to request
     if ($this->headers !== null && is_array($this->headers) && !empty($this->headers)) {
         $template->addHeaders($this->headers);
     }
     // Set the template
     Request::ini($template);
 }
Пример #13
0
 /**
  * Sends an HTTP request and returns a response.
  * 
  * @param string A valid API URI.
  * @return mixed Returns an empty string or an array of objects.
  */
 private static function sendRequest($uri)
 {
     if (RESULT_AS_ARRAY === true) {
         \Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));
     }
     $request = Request::get($uri);
     if (USE_CONTENT_NEGOTIATION === false) {
         $request->addHeaders(array('api-version' => API_VERSION, 'User-Agent' => USER_AGENT));
     } else {
         $request->addHeaders(array('Accept' => ACCEPT_HEADER, 'User-Agent' => USER_AGENT));
     }
     $response = $request->send();
     self::checkResponseCode($response);
     return $response->body;
 }