2011-09-01 60 views
0

我目前正在編寫一個C#windows服務,它與一個PHP頁面集成。我有一個代碼在PHP中進行請求的例子,但是我從來沒有在PHP中開發過,也不瞭解cURL函數如何執行請求。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; 
} 
+0

PHP函數'curl_getinfo'可以幫助你。 [Documentation here](http://www.php.net/manual/en/function.curl-getinfo.php) –

+1

附註:我認爲寫這個PHP代碼的人會混淆*多態*和*可變參數*。 – netcoder

+0

您可以創建一個簡單的套接字服務器並將請求發送到那裏進行分析嗎? – netcoder

回答

1

將選項CURLINFO_HEADER_OUT添加到curl句柄,然後在調用後調用curl_getinfo。

如:

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