getCurlOptions() public method

public getCurlOptions ( ) : array
return array
Exemplo n.º 1
0
 /**
  * Returns a response for specified HTTP request.
  *
  * @param Request $request HTTP Request to send.
  *
  * @return Response Response for specified request.
  */
 public function send(Request $request)
 {
     $ch = curl_init($request->getUrl());
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
     curl_setopt($ch, CURLOPT_HTTPHEADER, HttpUtil::formatHeadersForCurl($request->getHeaders()));
     if (!is_null($request->getBody())) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
     }
     curl_setopt_array($ch, $request->getCurlOptions());
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_FAILONERROR, false);
     curl_setopt($ch, CURLOPT_HEADER, true);
     list($status, $headers, $body) = HttpUtil::parseResponse(curl_exec($ch));
     return new Response(HttpUtil::parseStatus($status), HttpUtil::parseHeaders($headers), $body, curl_getinfo($ch));
 }
Exemplo n.º 2
0
 /**
  * Sets a cURL option on a Request.
  *
  * @param Request  $request Request to set cURL option to.
  * @param integer  $option  cURL option to set.
  * @param mixed    $value   Value of the cURL option.
  * @param resource $curlHandle cURL handle where this option is set on (optional).
  */
 public static function setCurlOptionOnRequest(Request $request, $option, $value, $curlHandle = null)
 {
     switch ($option) {
         case CURLOPT_URL:
             $request->setUrl($value);
             break;
         case CURLOPT_FOLLOWLOCATION:
             $request->getParams()->set('redirect.disable', !$value);
             break;
         case CURLOPT_MAXREDIRS:
             $request->getParams()->set('redirect.max', $value);
             break;
         case CURLOPT_CUSTOMREQUEST:
             $request->setMethod($value);
             break;
         case CURLOPT_POST:
             if ($value == true) {
                 $request->setMethod('POST');
             }
             break;
         case CURLOPT_POSTFIELDS:
             // todo: check for file @
             if (is_array($value)) {
                 foreach ($value as $name => $fieldValue) {
                     $request->setPostField($name, $fieldValue);
                 }
                 if (count($value) == 0) {
                     $request->removeHeader('Content-Type');
                 }
             } else {
                 $request->setBody($value);
             }
             break;
         case CURLOPT_HTTPHEADER:
             foreach ($value as $header) {
                 $headerParts = explode(': ', $header, 2);
                 if (isset($headerParts[1])) {
                     $request->setHeader($headerParts[0], $headerParts[1]);
                 }
             }
             break;
         case CURLOPT_HEADER:
         case CURLOPT_WRITEFUNCTION:
         case CURLOPT_HEADERFUNCTION:
             // Ignore writer and header functions.
             // These options are stored and will be handled later in handleOutput().
             break;
         case CURLOPT_READFUNCTION:
             // Guzzle provides a callback to let curl read the body string.
             // To get the body, this callback is called manually.
             $bodySize = $request->getCurlOptions()->get(CURLOPT_INFILESIZE);
             Assertion::notEmpty($bodySize, "To set a CURLOPT_READFUNCTION, CURLOPT_INFILESIZE must be set.");
             $body = call_user_func_array($value, array($curlHandle, fopen('php://memory', 'r'), $bodySize));
             $request->setBody($body);
             break;
         default:
             $request->getCurlOptions()->set($option, $value);
             break;
     }
 }