PHP cURL函数产生什么请求?


What request does the PHP cURL function produce?

我目前正在编写一个c# windows服务,它集成了一个PHP页面。我有一个代码的例子,使请求在PHP,但我从来没有开发过PHP,不明白cURL函数是如何执行请求。

是否有办法检索正在发送的请求?或者谁能提供一个示例,说明请求的外观和发送方式,以便我可以在c#中复制请求。

谢谢你的帮助。

public function api(/* polymorphic */) {
   $args = func_get_args();
   if (is_array($args[0])) {
     $serviceId = $this->getApiServiceId($args[0]["method"]);
     unset($args[0]["method"]);
     $args[0]["serviceId"] = $serviceId;      
     $args[0]["dealerId"] = $this->dealerId;
     $args[0]["username"] = $this->username;
     $args[0]["password"] = $this->password;
     $args[0]["baseDomain"] = $this->baseDomain;      
     return json_decode($this->makeRequest($args[0]));
   } else {
     throw Exception("API call failed. Improper call.");
  }
}
protected function makeRequest($params, $ch=null) {
   if (!$ch) {
      $ch = curl_init();
   }
   $opts = self::$CURL_OPTS;
   if ($this->useFileUploadSupport()) {
      $opts[CURLOPT_POSTFIELDS] = $params;
   } else {
      $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
   }
   // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait
   // for 2 seconds if the server does not support this header.
   if (isset($opts[CURLOPT_HTTPHEADER])) {
      $existing_headers = $opts[CURLOPT_HTTPHEADER];
      $existing_headers[] = 'Expect:';
     $opts[CURLOPT_HTTPHEADER] = $existing_headers;
   } else {
      $opts[CURLOPT_HTTPHEADER] = array('Expect:');
   }
   curl_setopt_array($ch, $opts);
   $result = curl_exec($ch);
   if ($result === false) {
      $e = new WPSApiException(array(
         'error_code' => curl_errno($ch),
         'error'      => array(
            'message' => curl_error($ch),
            'type'    => 'CurlException',
         ),
      ));
      curl_close($ch);
      throw $e;
   }
   curl_close($ch);
   return $result;
}

在curl句柄中添加CURLINFO_HEADER_OUT选项,然后在执行后调用curl_getinfo

:

//...
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
//...
curl_exec($ch);
//...
$header = curl_getinfo(CURLINFO_HEADER_OUT);
echo $header;