示例#1
0
 function server($test, $request_method, $data = [])
 {
     $this->curl->setHeader('X-DEBUG-TEST', $test);
     $request_method = strtolower($request_method);
     $this->curl->{$request_method}(self::TEST_URL, $data);
     return $this->curl->response;
 }
示例#2
0
 /**
  * @param array $data
  * @return int|mixed|null
  * @throws \ErrorException
  */
 protected function makeRequest(array $data = [])
 {
     $requestUrl = $this->getRequestURI();
     $result = null;
     switch ($this->requestHttpMethod()) {
         case self::METHOD_POST:
             $result = $this->curl->post($requestUrl, $data);
             break;
         case self::METHOD_GET:
             $result = $this->curl->get($requestUrl, $data);
             break;
         case self::METHOD_PUT:
             $result = $this->curl->put($requestUrl, $data);
             break;
         case self::METHOD_PATCH:
             $result = $this->curl->patch($requestUrl, $data);
             break;
         default:
             $this->curl->get($requestUrl, $data);
     }
     return $result;
 }
示例#3
0
 public function get($urlMixed, $data = [])
 {
     $this->data = $data;
     if (is_array($urlMixed)) {
         $curlMulti = curl_multi_init();
         $this->multiParent = true;
         $this->curls = [];
         foreach ($urlMixed as $url) {
             $curl = new Curl();
             $curl->multiChild = true;
             $curl->setOption(CURLOPT_URL, $this->buildURL($url, $data), $curl->curl);
             $curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
             $curl->setOption(CURLOPT_HTTPGET, true);
             $this->call($this->beforeSendCallback, $curl);
             $this->curls[] = $curl;
             $curlMultiErrorCode = curl_multi_add_handle($curlMulti, $curl->curl);
             if (!($curlMultiErrorCode === CURLM_OK)) {
                 throw new \ErrorException('cURL multi add handle error: ' . $curlMultiErrorCode);
             }
         }
         /** @var \edwardstock\curl\Curl $ch */
         foreach ($this->curls as $ch) {
             foreach ($this->options as $key => $value) {
                 $ch->setOption($key, $value);
             }
         }
         do {
             $status = curl_multi_exec($curlMulti, $active);
         } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
         foreach ($this->curls as $ch) {
             $this->exec($ch);
         }
     } else {
         $this->setOption(CURLOPT_URL, $this->buildURL($urlMixed, $data));
         $this->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
         $this->setOption(CURLOPT_HTTPGET, true);
         return $this->exec();
     }
     return null;
 }