/** * 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); }
/** * @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()); }
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); }
{ /** * 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());
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); }
<?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";
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; }
/** * 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); }
/** * 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; }