简体   繁体   中英

What request does the PHP cURL function produce?

I am currently writing an C# windows service, which integrates with a PHP page. I have an example of code making the request in PHP which is below however I have never developed in PHP and don't understand how the cURL function performs the request.

Is there anyway to retrieve the request which is being sent? Or can anyone provide an example of how the request would look and how the request is sent so I can replicate the request in C#.

Thank you for any help.

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;
}

Add the option CURLINFO_HEADER_OUT to curl handle, then call curl_getinfo on it after execing.

As in:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM