Example #1
0
 public static function createRequest($url, $method, $data = array(), $headers = array())
 {
     $methods = array(HttpMethod::GET, HttpMethod::POST, HttpMethod::PUT, HttpMethod::DELETE);
     if (!in_array($method, $methods)) {
         $method = HttpMethod::GET;
     }
     return Bin::getInstance()->request($url, $method, $data, $headers);
 }
Example #2
0
 public function run()
 {
     $bin = Bin::getInstance();
     $bin->extend('status', function ($vars = array()) {
         return new HttpStatus($vars);
     });
     $bin->extend('request', function ($url, $method, $data = array(), $headers = array()) {
         return new HttpRequest($url, $method, $data, $headers);
     });
     $bin->extend('response', function ($response, $content) {
         return new HttpResponse($response, $content);
     });
 }
Example #3
0
 public function send($dataSet = array(), $headers = array())
 {
     $context = array('timeout' => 60, 'max_redirects' => 10, 'protocol_version' => 1.1);
     switch (strtoupper(trim($this->method))) {
         case HttpMethod::GET:
             $context['method'] = HttpMethod::GET;
             break;
         case HttpMethod::POST:
             $context['method'] = HttpMethod::POST;
             $headers['content-type'] = 'application/x-www-form-urlencoded';
             break;
         case HttpMethod::PUT:
             $context['method'] = HttpMethod::POST;
             $dataSet['__method'] = HttpMethod::PUT;
             $headers['content-type'] = 'application/x-www-form-urlencoded';
             break;
         case HttpMethod::DELETE:
             $context['method'] = HttpMethod::POST;
             $dataSet['__method'] = HttpMethod::DELETE;
             break;
         default:
             $context['method'] = HttpMethod::GET;
             break;
     }
     $context['header'] = $this->getNormalizedHeaders($headers);
     $context['content'] = $this->getNormalizedData($dataSet);
     try {
         $url = $this->getNormalizedUrl();
         $context = stream_context_create(array('http' => $context));
         $stream = fopen($url, 'r', false, $context);
         $content = stream_get_contents($stream);
         $response = stream_get_meta_data($stream);
         fclose($stream);
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage());
     }
     return Bin::getInstance()->response($response, $content);
 }