[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/httpful/examples/ -> override.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1