public function testGetExtractInfo()
 {
     $response = new Response();
     $response->setExtractInfo(array('http_code' => 200));
     $this->assertArrayHasKey('http_code', $response->getExtractInfo());
 }
Example #2
0
 /**
  * 原始发送请求
  * @param string $url 完整URL
  * @param string|array $bodyParam body请求体。$requestMethod为POST时有效
  * @param string $requestMethod 请求方法,必须全大写
  * @param Response $response
  * @return Response $response
  */
 public function rawSend($url, $bodyParam = null, $requestMethod = 'GET', Response $response = null)
 {
     if (null === $response) {
         $response = new Response();
     }
     if (null === $this->curlInit) {
         $this->curlInit = curl_init();
     }
     $curlOpt = $this->getDefaultCurlOpt();
     $curlOpt[CURLOPT_URL] = $url;
     if ($requestMethod == 'POST') {
         $curlOpt[CURLOPT_POST] = true;
     }
     $curlOpt[CURLOPT_CUSTOMREQUEST] = $requestMethod;
     if ($requestMethod == 'POST' || $requestMethod == 'PUT') {
         if (is_array($bodyParam)) {
             if (!$this->rawSendCheckHasFile($bodyParam)) {
                 $bodyParam = http_build_query($bodyParam);
             } else {
                 $bodyParam = $this->rawSendBuildCleanUploadBody($bodyParam);
             }
         }
         if ($bodyParam !== null && $bodyParam !== "") {
             $curlOpt[CURLOPT_POSTFIELDS] = $bodyParam;
         } else {
             $curlOpt[CURLOPT_POSTFIELDS] = "";
         }
     }
     curl_setopt_array($this->curlInit, $curlOpt);
     $rawResult = curl_exec($this->curlInit);
     $curlInfo = curl_getinfo($this->curlInit);
     $curl_errno = curl_errno($this->curlInit);
     if ($curl_errno) {
         $response->setError("CURL_ERROR", curl_error($this->curlInit) . '[ErrCode ' . $curl_errno . ']');
     } else {
         $response->create($curlInfo['http_code'], $rawResult);
     }
     $response->setExtractInfo($curlInfo);
     if (!empty($this->requestLoggerStack)) {
         $this->dispatchRequestLogger($url, isset($curlOpt[CURLOPT_POSTFIELDS]) ? $curlOpt[CURLOPT_POSTFIELDS] : null, $requestMethod, $response);
     }
     return $response;
 }