Example #1
0
 private function initCurl()
 {
     $this->curl = curl_init($this->request->getUrl());
     foreach ($this->request->getOptions() as $option) {
         curl_setopt($this->curl, $option[0], $option[1]);
     }
     $this->curlResponse = curl_exec($this->curl);
 }
 public function send(Request $request)
 {
     $this->setOptions($request->getOptions());
     switch ($this->method) {
         case CURL::DELETE:
             $this->addOption(CURLOPT_CUSTOMREQUEST, CURL::DELETE);
             break;
         case CURL::PUT:
             $this->addOption(CURLOPT_PUT, true);
             $this->addOption(CURLOPT_POST, $request->getPayload());
             break;
         case CURL::POST:
             /**
              * curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
              * curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);        
              * curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              * 'Content-Type: application/json',
              * 'Content-Length: ' . strlen($data_string))                                                                       
              * );                                                                                                                    
              */
             $this->addOption(CURLOPT_CUSTOMREQUEST, CURL::POST);
             $this->addOption(CURLOPT_POSTFIELDS, $request->getPayload());
             $this->addOption(CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'Content-Length: ' . strlen($request->getPayload())));
             break;
     }
     curl_setopt_array($this->handler, $this->getOptions());
     $response = Response::newResponse(curl_exec($this->handler), curl_getinfo($this->handler), curl_error($this->handler));
     return $response;
 }
Example #3
0
 /**
  * Construct response object from Request
  *
  * @param   Request $request
  * @access    public
  * @throws  \RuntimeException
  */
 public function __construct(Request $request)
 {
     $options = $request->getOptions();
     $headers = $request->getHeaders();
     if (!isset($options[CURLOPT_URL])) {
         throw new \RuntimeException("the URL for the request is not set");
     }
     $handle = curl_init();
     if (!$handle) {
         throw new \RuntimeException("failed to initialized cURL");
     }
     foreach ($options as $option => $value) {
         if ($option) {
             if (!curl_setopt($handle, $option, $value)) {
                 throw new \RuntimeException("failed to set option {$option} => {$value}");
             }
         }
     }
     if (count($headers)) {
         if (!curl_setopt($handle, CURLOPT_HTTPHEADER, $headers)) {
             throw new \RuntimeException("malformed headers collection");
         }
     }
     $this->data = curl_exec($handle);
     $this->error = ["errno" => curl_errno($handle), "error" => curl_error($handle)];
     if ($this->error["errno"]) {
         throw new \RuntimeException("failed to execute request for {$options[CURLOPT_URL]}", $this->error["errno"]);
     }
     $this->info = curl_getinfo($handle);
     curl_close($handle);
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public function request(Request $request, $callback)
 {
     $crawler = $this;
     $request->setOptions(\array_merge($this->getOptions(), $request->getOptions()));
     $this->tasksIndex++;
     $this->tasks[$this->tasksIndex] = new Task($this->tasksIndex, $request, new Response($request->url), function (Task $task) use($crawler, $callback) {
         if ($callback && \is_callable($callback)) {
             \call_user_func_array($callback, [$task->getRequest(), $task->getResponse()]);
         }
         $crawler->remove($task);
     });
     return $this->tasks[$this->tasksIndex];
 }
Example #5
0
 public function __construct()
 {
     $this->route = (object) array();
     // Controller name
     $controller = Request::get('controller') ? Request::get('controller') : 'home';
     $this->route->controller = $controller;
     Request::$get->controller = $controller;
     $method = Request::get('method') ? Request::get('method') : 'index';
     $this->route->method = $method;
     Request::$get->method = $method;
     if (Request::get('ident') !== NULL) {
         $this->route->ident = Request::get('ident');
     }
     if (Request::getOptions() !== NULL) {
         $this->route->options = Request::getOptions();
     }
     $this->load();
 }
Example #6
0
 /**
  * Prepare the curl resource for sending a request.
  *
  * @param  Request $request
  *
  * @return void
  */
 public function prepareRequest(Request $request)
 {
     $this->ch = curl_init();
     curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($this->ch, CURLOPT_HEADER, true);
     if ($request->getUserAndPass()) {
         curl_setopt($this->ch, CURLOPT_USERPWD, $request->getUserAndPass());
     }
     curl_setopt($this->ch, CURLOPT_URL, $request->getUrl());
     $options = $request->getOptions();
     if (!empty($options)) {
         curl_setopt_array($this->ch, $options);
     }
     $method = $request->getMethod();
     if ($method === 'post') {
         curl_setopt($this->ch, CURLOPT_POST, 1);
     } elseif ($method !== 'get') {
         curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
     }
     curl_setopt($this->ch, CURLOPT_HTTPHEADER, $request->formatHeaders());
     if ($this->methods[$method] === true) {
         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request->encodeData());
     }
 }
Example #7
0
 /**
  * Helper function to gather all the curl options: global, inferred, and per request
  *
  * @param Request $request
  * @return array
  */
 private function prepareRequestOptions(Request $request)
 {
     // options for this entire curl object
     $options = $this->getOptions();
     // set the request URL
     $options[CURLOPT_URL] = $request->getUrl();
     // set the request method
     $options[CURLOPT_CUSTOMREQUEST] = $request->getMethod();
     // posting data w/ this request?
     if ($request->getPostData()) {
         $options[CURLOPT_POST] = 1;
         $options[CURLOPT_POSTFIELDS] = $request->getPostData();
     }
     // if the request has headers, use those, or if there are global headers, use those
     if ($request->getHeaders()) {
         $options[CURLOPT_HEADER] = 0;
         $options[CURLOPT_HTTPHEADER] = $request->getHeaders();
     } elseif ($this->getHeaders()) {
         $options[CURLOPT_HEADER] = 0;
         $options[CURLOPT_HTTPHEADER] = $this->getHeaders();
     }
     // if the request has options set, use those and have them take precedence
     if ($request->getOptions()) {
         $options = $request->getOptions() + $options;
     }
     return $options;
 }