【发布时间】:2014-02-12 00:59:53
【问题描述】:
我有一个 nginx 重写规则,将 img src 属性重定向到 php 页面。在这个 php 页面中,我正在尝试发出 GET 请求,该请求成功后会向同一页面发出 POST 请求,将从 GET 请求返回的数据作为数据发送。为什么 php 脚本中的 $_POST 数据为空?如果我在 php 脚本中对 $name = "http://path/to/my/img.png" 进行硬编码,图像会正确呈现。
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
var_dump($_REQUEST);
//if(isset($_POST['val'])) {
// open the file in a binary mode
$name = $_POST['val']; // ALWAYS EMPTY
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
//echo fpassthru($fp);
header("Location: $name");
exit;
//}
?>
<html>
<head>
<script type='text/javascript' src='/steal/steal.js'></script>
<script type="text/javascript" src="/plugins/jquery/json2.js"></script>
<script type="text/javascript">
steal('jquery/dom/fixture').then(function(){
$.fixture("GET /event/{code}", function(original, settings, headers){
return [200, "success", { "img_url":"http://path/to/my/img.png" }, {} ]
})
var strObj = <?php echo json_encode($_REQUEST); ?>;
var str = strObj.q;
var eventCode = str.split('/')[1];
$.ajax({
url: "/event/"+eventCode,
success: function(data) {
var imgUrl = data.img_url
$.ajax({
type: 'POST',
contentType: 'json',
data: {val:imgUrl},
success: function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
}
});
}
});
});
</script>
</head>
<body>
</body>
</html>
【问题讨论】:
-
好的,等一下。你试图让图像的 src 指向这个 php 页面,在那个 php 页面内你输出 javascript(据称是为了让用户浏览器运行),然后将一个 POST 请求与他们的数据发送到相同的 url刚刚进入 GET?
-
这用于电子邮件跟踪。基本上,用户将收到一封电子邮件,并在打开该电子邮件时 img src 属性向 uri 发出请求,然后将其重写为 php 脚本。 php 脚本接收 $_REQUEST 数据——包含一些与用户绑定的 id,使用该 id 发出 GET 请求,成功时返回一个 url。从这里开始,url 被用作对同一 php 页面的 POST 请求的数据,该页面将被 base64 编码并作为 img src 属性返回。这听起来对我来说也很复杂,所以我愿意接受建议。