2012-04-07 79 views
1

我做的URL进行文件的file_get_contents做汽车url_decode

file_get_contents('https://xyz.com/login.php?app_data=%7B%22page%22%3A%22details%22%2C%22id%22%3A%2273%22%2C%22crp%22%3A%221%22%2C%22cip%22%3A%22%22%7D'); 

如下因素,但我在另一端接收它作为

app_data={"page":"details","id":"73","crp":"1","cip":""} 

,而不是

app_data=%7B%22page%22%3A%22details%22%2C%22id%22%3A%2273%22%2C%22crp%22%3A%221%22%2C%22cip%22%3A%22%22%7D' 

另一端的代码:

if(isset($_GET['url'])) 
{ 
    log($_GET['url']); 
} 

log只写入文件。

回答:没有,但$_GET确实

+0

你是如何接收它的?用'$ _GET ['app_data']'? – 2012-04-07 10:32:22

回答

1

按文档的$_GET

注:

GET变量通过urldecode()传递。

这适用于键和值(后者是什么导致你的困惑)。为了得到你想要的结果,你要么需要双编码在客户端上(这是一个非常丑陋的解决方法):

$appdata = urlencode(urlencode('{"page":"details","id":"73","crp":"1","cip":""}')); 
file_get_contents("https://xyz.com/login.php?app_data=$appdata"); 

或者只是做正确和后处理在服务器端的字符串:

// $app_data will contain the nice, unescaped form 
$app_data = $_GET['app_data'] 

// later if we need to pass $app_data in another request, 
// we explicitly encode it again. 
$app_data_encoded = urlencoded($app_data); 
+0

好吧,基本上我认为我们同意;有两种方法可以使OP工作:或者在呼叫者处使用DOUBLE-'urlencode'字符串,或者在呼叫者中将其编码为一次,并在接收器中将其编码为一次。 – 2012-04-07 10:51:23

+0

@ Dr.Kameleon:是的。我想要清理关于实际发生解码的*的混淆。 Web服务器不*解码它们,'file_get_contents'也不解码! – 2012-04-07 10:52:48

0

您可以使用urlencode返回编码形式。

$result = urlencode($app_data); 
echo $result; 
+0

这也可以工作... – 2012-04-07 10:46:28

1

解决方案A:

编码您appdata两次来电脚本

// Caller script 
$appdata = '"page":"details","id":"73","crp":"1","cip":""'; 
$appdata = urlencode(urlencode($appdata)); 

file_get_contents("https://xyz.com/login.php?app_data=$appdata"); 

解决方案B:

编码您appdata一旦调用者脚本,一旦在接收器脚本

// Caller script 
$appdata = '"page":"details","id":"73","crp":"1","cip":""'; 
$appdata = urlencode($appdata); 

file_get_contents("https://xyz.com/login.php?app_data=$appdata"); 

// Receiver script 
$appdata = urlencode($_GET['app_data']); 

什么,你会得到的是你所期望的...... ;-)

+0

我已经urlencoding它 – aWebDeveloper 2012-04-07 10:40:45

+0

@Web开发者:他URL编码*两次*。 – 2012-04-07 10:41:03

+0

taht's在应用数据中添加一个“”“ – aWebDeveloper 2012-04-07 10:41:42