override.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. require(__DIR__ . '/../bootstrap.php');
  3. // We can override the default parser configuration options be registering
  4. // a parser with different configuration options for a particular mime type
  5. // Example setting a namespace for the XMLHandler parser
  6. $conf = array('namespace' => 'http://example.com');
  7. \Httpful\Httpful::register(\Httpful\Mime::XML, new \Httpful\Handlers\XmlHandler($conf));
  8. // We can also add the parsers with our own...
  9. class SimpleCsvHandler extends \Httpful\Handlers\MimeHandlerAdapter
  10. {
  11. /**
  12. * Takes a response body, and turns it into
  13. * a two dimensional array.
  14. *
  15. * @param string $body
  16. * @return mixed
  17. */
  18. public function parse($body)
  19. {
  20. return str_getcsv($body);
  21. }
  22. /**
  23. * Takes a two dimensional array and turns it
  24. * into a serialized string to include as the
  25. * body of a request
  26. *
  27. * @param mixed $payload
  28. * @return string
  29. */
  30. public function serialize($payload)
  31. {
  32. $serialized = '';
  33. foreach ($payload as $line) {
  34. $serialized .= '"' . implode('","', $line) . '"' . "\n";
  35. }
  36. return $serialized;
  37. }
  38. }
  39. \Httpful\Httpful::register('text/csv', new SimpleCsvHandler());