White space problem while using php proxy
I'm using a php web proxy with my URL already encoded and keep having getting a malformed request error once I have any text after a %20. Any idea why this would be happening? The web proxy code I'm using is just a sample that I took from yahoo services:
<?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 th开发者_运维百科e 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);
?>
It turned out the issue was that I needed to url encode the parameter in my url that contained characters that might need url encoding, such as the whitespace. The value of yws_path was already getting url encoded, but I think this must be getting decoded by the proxy and then sent along the http request. Without double encoding the portion where I needed a whitespace, my request wasn't getting passed along with a %20 but instead with just a whitespace (essentially resulting in a http request that wasn't properly url encoded).
精彩评论