Curl\Curl::exec PHP Method

exec() public method

Exec
public exec ( $ch = null ) : mixed
$ch
return mixed Returns the value provided by parseResponse.
    public function exec($ch = null)
    {
        if ($ch === null) {
            $this->responseCookies = array();
            $this->call($this->beforeSendFunction);
            $this->rawResponse = curl_exec($this->curl);
            $this->curlErrorCode = curl_errno($this->curl);
            $this->curlErrorMessage = curl_error($this->curl);
        } else {
            $this->rawResponse = curl_multi_getcontent($ch);
            $this->curlErrorMessage = curl_error($ch);
        }
        $this->curlError = !($this->curlErrorCode === 0);
        // Include additional error code information in error message when possible.
        if ($this->curlError && function_exists('curl_strerror')) {
            $this->curlErrorMessage = curl_strerror($this->curlErrorCode) . (empty($this->curlErrorMessage) ? '' : ': ' . $this->curlErrorMessage);
        }
        $this->httpStatusCode = $this->getInfo(CURLINFO_HTTP_CODE);
        $this->httpError = in_array(floor($this->httpStatusCode / 100), array(4, 5));
        $this->error = $this->curlError || $this->httpError;
        $this->errorCode = $this->error ? $this->curlError ? $this->curlErrorCode : $this->httpStatusCode : 0;
        // NOTE: CURLINFO_HEADER_OUT set to true is required for requestHeaders
        // to not be empty (e.g. $curl->setOpt(CURLINFO_HEADER_OUT, true);).
        if ($this->getOpt(CURLINFO_HEADER_OUT) === true) {
            $this->requestHeaders = $this->parseRequestHeaders($this->getInfo(CURLINFO_HEADER_OUT));
        }
        $this->responseHeaders = $this->parseResponseHeaders($this->rawResponseHeaders);
        $this->response = $this->parseResponse($this->responseHeaders, $this->rawResponse);
        $this->httpErrorMessage = '';
        if ($this->error) {
            if (isset($this->responseHeaders['Status-Line'])) {
                $this->httpErrorMessage = $this->responseHeaders['Status-Line'];
            }
        }
        $this->errorMessage = $this->curlError ? $this->curlErrorMessage : $this->httpErrorMessage;
        if (!$this->error) {
            $this->call($this->successFunction);
        } else {
            $this->call($this->errorFunction);
        }
        $this->call($this->completeFunction);
        // Close open file handles and reset the curl instance.
        if (!($this->fileHandle === null)) {
            $this->downloadComplete($this->fileHandle);
        }
        return $this->response;
    }

Usage Example

Example #1
0
 /**
  *	Выполнить запрос
  *
  *	@param	mixed	$ch
  */
 public function exec($ch = null)
 {
     if ($this->headers) {
         $headers = array_filter($this->headers, 'strlen');
         $this->setOpt(CURLOPT_HTTPHEADER, array_map(function ($value, $key) {
             return $key . ': ' . $value;
         }, $headers, array_keys($headers)));
     }
     $response = parent::exec($ch);
     // TODO совместимость с другими API
     if ($this->http_status_code && isset($this->exceptions[$this->http_status_code])) {
         $response['error'] = true;
         $response['message'] = implode('; ', $this->response);
     }
     if (isset($response['error'])) {
         $exception = 'Exception';
         if (isset($this->exceptions[$this->error_code])) {
             $exception = 'Mackey\\Yandex\\Exception\\' . $this->exceptions[$this->error_code];
         }
         try {
             throw (new \ReflectionClass($exception))->newInstance($response['message'], $this->error_code);
         } catch (\ReflectionException $exc) {
             throw new \Exception($response['message'], $this->error_code, $exc);
         }
     }
     return $response;
 }