开发者

PHP proxy to get XML: problem with URL arguments

So I need to retrieve XML from a public API, but my app is in Flash and the public service won't implement a crossdomain.xml file. I found this PHP script online (below) for a proxy to request the URL. It works fine for URLs like:

http://mysite.com/xml_proxy.php?url=http://rss.cnn.com/rss/cnn_开发者_JAVA百科topstories.rss

but the script seems to either strip or ignore any arguments on the URL, like:

http://mysite.com/xml_proxy.php?url=http://publicapiserver.com?app_ID=35235x&app_key=84x

I'm PHP ignorant. Is there an easy way to have this script process those URL arguments? Thanks very much. Here is the script:

<?php

$post_data = $HTTP_RAW_POST_DATA;

$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

$ch = curl_init( $_GET['url'] ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ( strlen($post_data)>0 ){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}

$response = curl_exec($ch);     
$response_headers = curl_getinfo($ch);     

if (curl_errno($ch)) {
    print curl_error($ch);
} else {
    curl_close($ch);
    header( 'Content-type: ' . $response_headers['content-type']);
    print $response;
}
?>


If you want that URL to get through to the script unscathed as a URL parameter, you'll have to url-encode it first, at the Flash side. For example, the correct URL to use to get to your script for your example is actually:

http://mysite.com/xml_proxy.php?url=http%3A%2F%2Fpublicapiserver.com%3Fapp_ID%3D35235x%26app_key%3D84x

Otherwise your web server and PHP will get very confused about which bit of your URL is the URL for the proxy service, and which bit is the URL that you want to go get data from.

The solution from the Flash side probably depends on how you're doing the call at the moment. But Actionscript has an escape() function which will do the encoding for you. You only want to encode the URL you're sending to the proxy, not the URL of the proxy itself, so leave the proxy URL as it is ("http://mysite.com/xml_proxy.php?url=") and then throw the escape()'d URL of the site you want to get data from on the end of it.

You don't need to do anything from the PHP end, because PHP automatically url-decodes the parameters in its $_GET variable, which is how you're grabbing that URL in the PHP code.


Add this line if you need to proxy a soap client :

$header[] = "SOAPAction: ".$_SERVER['HTTP_SOAPACTION'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜