2015-09-05 38 views
-1

的Windows NT 6.2 DEV01構建9200(未知的Windows版本標準版)i586的 PHP版本5.4.24 了allow_url_fopen =在捲曲400形成嚴重要求

的URL我用這樣的容貌:

http://www.warrantycompany.com/services/rate.asmx/GetRates?AccountNumber=37920N&DRFC=0&Mileage=52000&VIN=1G2ZA5EK3A4163364&ManufacturerWarranty=y&BrandedOrSalvaged=branded&EngineCC=0 

及相關PHP代碼如下所示:

$curlSession = curl_init(); 
curl_setopt($curlSession, CURLOPT_TIMEOUT , 30); 

curl_setopt($curlSession, CURLOPT_HTTPGET, 1); 
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlSession, CURLOPT_URL, $url); 

$sTemp = curl_exec($curlSession); 

echo curl_errno($curlSession); 
echo curl_error($curlSession); 

直接加載到瀏覽器中,我得到XML的長塊與INF我想要的信息。然而,通過cURL,我得到「HTTP錯誤400.請求形成嚴重。」

如果我更改URL閱讀

http://www.warrantycompany.com/services/rate.asmx/GetRates/?AccountNumber=... 

(注意問號之前的斜線),我會得到相同的。

如果我使用的參數進行urlencode()將URL改爲

http://www.warrantycompany.com/services/rate.asmx/GetRates?AccountNumber%3D37920N%26DRFC%3D0%26... 

,請求工作,但在遠程系統出現了錯誤。

file_get_contents返回「false」。

這個工作的唯一方法 - 迄今爲止工作 - 是使用服務器對象「MSXML2.ServerXMLHTTP」,如果可能,我不想這樣做,但我很茫然。

+0

當我到瀏覽器複製URL,我得到一個錯誤消息和404也許你應該先檢查一下服務器應用程序到底是接受試圖亂動前未知參數等。這看起來像一個API,應該有某種文檔。 –

+0

是的,你會得到一個404錯誤:我的例子中的網址使用了一個虛假的域名,因爲這是我工作的代碼。正如我在文中所說,直接加載到瀏覽器中,我使用的URL(實際的URL)工作得很好;我用XML獲得所需的信息。 –

回答

0

試試這個..

//put your parameters in a array with key/value like this 
$arr = array('AccountNumber' => 37920, 'DRFC' => 0,......); 

//set the base url of the service 
$url = 'http://www.warrantycompany.com/services/rate.asmx'; 

$uri = http_build_query($arr, '', '&'); 
$uri = str_replace (array('+',' '), '%20', $uri); 
$url = sprintf("%s?%s", $url, $uri); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_TIMEOUT , 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
$sTemp = curl_exec($ch); 

echo curl_errno($ch); 
echo curl_error($ch); 
curl_close ($ch); 


//you can handle the respond like this 
list($header, $body) = explode("\r\n\r\n", $respond, 2); 
+0

不幸的是,這個例子省略了URL的關鍵部分,無論如何,是的,我已經用編碼嘗試過了,正如你在原始描述中看到的那樣。 –