代码之家  ›  专栏  ›  技术社区  ›  SimonDowdles

php cURL发布如何跟踪位置

  •  1
  • SimonDowdles  · 技术社区  · 14 年前

        $ch = curl_init("http://localhost/eterniti/cart-step-1.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "error=1&em=$em&fname=$fname&lname=$lname&email1=$email1&email2=$email2&code=$code&area=$area&number=$num&mobile=$mobile&address1=$address1&address2=$address2&address3=$address3&suburb=$suburb&postcode=$postcode&country=$country");
        curl_exec($ch);
        curl_close($ch);
    

    帖子运行良好,我被带到cart-step-1.php,在那里我可以处理发布的数据,但是浏览器的URL地址栏中的位置仍然是脚本页面的位置,在本例中是proc_xxxxxx.php

    有什么办法让URL地址反映我实际发布到的页面吗?

    4 回复  |  直到 14 年前
        1
  •  1
  •   Alain    12 年前

    您正在寻找:

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
        2
  •  1
  •   ninaj    12 年前

    curl_getinfo 要获取上次重定向到的URL,请执行以下操作:

    $url = curl_getinfo($ch , CURLINFO_EFFECTIVE_URL);
    

    然后使用 header

        3
  •  0
  •   chris    14 年前

    在这种情况下,您需要用html来完成它——因此proc_xxxxxx.php返回一个html页面,该页面包含一个实际发送post数据的表单。

    你现在做的是检索一个特定的页面(带参数) 并将其作为浏览器请求的页的数据返回。您的浏览器仍然认为它名为proc_xxxxxx.php,但它从服务器返回的html数据是cart-step-1.php请求的数据,但您的浏览器无法知道这一点。

    将浏览器栏中的url设置为任意值是不可能的,因为这将是一个巨大的安全风险(例如 window.url.display = 'http://www.ebay.com/login' ):天

        4
  •  0
  •   Felix Kling    14 年前

    curl 可以重定向用户的内容 header() :

    header('Location: http://localhost/eterniti/cart-step-1.php');
    

    更完整:

    $url = "http://localhost/eterniti/cart-step-1.php";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "error=1&em=$em&fname=$fname&lname=$lname&email1=$email1&email2=$email2&code=$code&area=$area&number=$num&mobile=$mobile&address1=$address1&address2=$address2&address3=$address3&suburb=$suburb&postcode=$postcode&country=$country");
    curl_exec($ch);
    curl_close($ch);
    
    header('Location: ' . $url);
    exit;
    

    根据此站点对参数的期望,这可能有效,也可能无效。如果您将浏览器重定向到此页,它将只是 GET 来自浏览器的请求。您不能让浏览器发送 POST


    我唯一想到的是(如果你想让浏览器发送 岗位 数据)是:
    不要使用curl,而是创建一个隐藏字段中包含所有值的表单,并显示一个按钮 Continue