2012-01-05 77 views
0
$returnurl = "http://clietsite.com:9000/service"; 
    $curlPost = "rtyinstance=<parent><child>value</child></parent>"; 
    $ch1 = curl_init(); 
    if (!$ch1) die("Couldn't initialize a cURL handle"); 
    $headerinfo = apache_request_headers(); 
    curl_setopt($ch1, CURLOPT_URL, $returnurl); 
    curl_setopt($ch1, CURLOPT_HEADER, 0); 
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch1, CURLOPT_POST, true); 
    curl_setopt($ch1, CURLOPT_POSTFIELDS, $curlPost); 
    curl_setopt($ch1, CURLOPT_FRESH_CONNECT, 1); 
    curl_setopt($ch1, CURLOPT_USERAGENT, $headerinfo['User-Agent']); 
    curl_setopt($ch1, CURLOPT_TIMEOUT,1*60); 
    $result = curl_exec($ch1); 
    var_dump($result); 
    echo $responseCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE); 
    curl_close($ch1); 

上述代碼返回responseCode 200,var dump打印字符串(0)「」。PHP中的CURL執行

當我在瀏覽器中運行

(我使用的PHP版本5.2.17) 像

http://clietsite.com:9000/service?instance=<parent><child>value</child></parent> 

併成功返回的XML。不知道可能是什麼問題。請幫我解決這個問題。

+0

仔細檢查您發送給該服務的輸入是否處於預期格式,例如,你可能想爲['CURLOPT_POSTFIELDS'](http://php.net/curl_setopt)使用'array',可能是因爲你不需要處理編碼後的值。 – hakre 2012-01-05 09:08:42

+0

我定義了一個數組$ postArray = array(); $ postArray ['rtyinstance'] = $ currentXML;並使用像curl_setopt($ ch1,CURLOPT_POSTFIELDS,$ postArray); – Arasu 2012-01-05 09:12:20

+0

但返回空 – Arasu 2012-01-05 09:12:36

回答

0

通過cURL您正在使用POST,而在瀏覽器中您發出一個GET。

這可能是問題所在。

要「轉換」,以獲得,使用此代碼:

$encoded_params = urlencode('<parent><child>value</child></parent>'); 
$returnurl = "http://clietsite.com:9000/service?rtyinstance=" . $encoded_params; 
$ch1 = curl_init(); 
if (!$ch1) die("Couldn't initialize a cURL handle"); 
$headerinfo = apache_request_headers(); 
curl_setopt($ch1, CURLOPT_URL, $returnurl); 
curl_setopt($ch1, CURLOPT_HEADER, 0); 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch1, CURLOPT_FRESH_CONNECT, 1); 
curl_setopt($ch1, CURLOPT_USERAGENT, $headerinfo['User-Agent']); 
curl_setopt($ch1, CURLOPT_TIMEOUT,1*60); 
$result = curl_exec($ch1); 
var_dump($result); 
echo $responseCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE); 
curl_close($ch1); 
+0

我如何通過作爲獲取字段捲曲 – Arasu 2012-01-05 09:08:24

+0

@Arasu:我更新了我的答案 – 2012-01-05 09:12:35

+1

[如何從POST切換到GET在PHP CURL](http://stackoverflow.com/questions/1225409/how-to -switch-post-to-get-in-php-curl) – hakre 2012-01-05 09:12:43

-1

以下網址不適用於我:

http://clietsite.com:9000/service?rtyinstance=<parent><child>value</child></parent> 

是該網站的真正訪問您的計算機/網絡之外?如果您將腳本託管在一臺服務器上,而另一臺上的網站則可能無法使用。

+0

我指定了示例站點名稱和端口 – Arasu 2012-01-05 09:13:33

+0

對不起,雖然您的問題尚不清楚。 – Magnus 2012-01-10 19:59:04