【问题标题】:Pass url property through a proxy通过代理传递 url 属性
【发布时间】:2010-11-24 18:04:13
【问题描述】:

我正在运行代理,因此我可以通过 url 参数对数据执行 ajax 请求。代理 php 看起来像:

<?php
header('Content-type: application/xml');
$daurl = 'http://thesite.com/form.asp';
$handle = fopen($daurl, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>

我正在使用 ajax 访问代理,最终附加了如下参数:

$j.ajax({
            type: 'GET',
            url: 'sandbox/proxy.php',
            data: 'order=' + ordervalue,
            dataType: 'html',
            success: function(response) {
            $j("#result").html(response);
            }
        });

所以请求就像是 sandbox/proxy.php?order=123

如何获取该数据 (order=123) 并将其附加到 $daurl 变量 (http://thesite.com/form.asp?order=123) 以便让代理实际返回一些内容?

这对我来说是处女地,所以你不能过度解释=)

【问题讨论】:

    标签: php ajax proxy


    【解决方案1】:

    简单。

    $daurl = 'http://thesite.com/form.asp';
    
    //if you only want 'order':
    if(isset($_GET['order'])) 
       $daurl .= '?order=' . $_GET['order'];
    
    //if you want the entire query string:
    
    if(strlen($_SERVER['QUERY_STRING']) > 0) 
       $daurl .= '?' . $_SERVER['QUERY_STRING'];
    ...
    

    【讨论】:

    • 太棒了!非常感谢。
    【解决方案2】:

    $_SERVER['QUERY_STRING'] 应该包含 order=123,因此您可以按如下方式更改 $daurl:

    $daurl = 'http://thesite.com/form.asp';
    if($_SERVER['QUERY_STRING'] != ""){
        $daurl.='?'.$_SERVER['QUERY_STRING'];
    }
    

    这样做将传输查询字符串上传递的所有内容。但是,如果您只需要可以使用 $_GET['order'] 的订单部分,则可能需要执行以下操作:

    $order = isset($_GET['order']) ? $_GET['order'] : -1;
    

    如果订单没有在查询字符串中传递,$order 将为 -1,否则它将具有值。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多