2010-05-20 76 views
0

我在使用已經編碼了URL的php web代理,並且在%20之後有任何文本時不斷收到格式錯誤的請求錯誤。任何想法爲什麼會發生?我使用的Web代理代碼僅僅是我從雅虎的服務了一個例子:使用php代理時出現空白區域問題

<?php 
// PHP Proxy example for Yahoo! Web services. 
// Responds to both HTTP GET and POST requests 
// 
// Author: Jason Levitt 
// December 7th, 2005 
// 

// Allowed hostname (api.local and api.travel are also possible here) 
define ('HOSTNAME', 'http://search.yahooapis.com/'); 

// Get the REST call path from the AJAX application 
// Is it a POST or a GET? 
$path = ($_POST['yws_path']) ? $_POST['yws_path'] : $_GET['yws_path']; 
$url = HOSTNAME.$path; 

// Open the Curl session 
$session = curl_init($url); 

// If it's a POST, put the POST data in the body 
if ($_POST['yws_path']) { 
$postvars = ''; 
while ($element = current($_POST)) { 
    $postvars .= urlencode(key($_POST)).'='.urlencode($element).'&'; 
    next($_POST); 
} 
curl_setopt ($session, CURLOPT_POST, true); 
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars); 
} 

// Don't return HTTP headers. Do return the contents of the call 
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

// Make the call 
$xml = curl_exec($session); 

// The web service returns XML. Set the Content-Type appropriately 
//header("Content-Type: text/xml"); 

echo $xml; 
curl_close($session); 

?> 
+0

「一旦我在%20之後有任何文本時仍會收到格式錯誤的請求錯誤」%20在哪裏?你能給個例子嗎? – Artefacto 2010-05-20 09:00:56

+0

http://mywebserver/php_proxy_simple.php?yws_path = GetData%3Flocation%3DUnited%20States 注意:mywebserver和GetData?只是組成,但它是我的真實代碼相同的格式。 如果我擺脫了「國家」,它會奏效。 如果我在%20之後添加任何附加字母,它會給我那個錯誤。 – KCC 2010-05-20 19:19:16

回答

0

原來的問題是,我需要的URL編碼在我的網址包含可能需要的URL字符的參數編碼,如空白。 yws_path的值已經獲得了url編碼,但我認爲這必須由代理解碼,然後沿着http請求發送。如果沒有對需要空格的部分進行雙重編碼,我的請求不會與%20一起傳遞,而只會帶有空白(實質上導致http請求未正確地進行url編碼)。