【问题标题】:Header() substitute标头()替代
【发布时间】:2011-11-21 11:33:08
【问题描述】:

您好,我是 php 新手,想知道 header('location:mysit.php'); 的一些替代函数

我正在发送这样的请求:

header('Location: http://localhost/(some external site).php'&?var='test')

类似的事情,但我想做的是我想将变量的值发送到外部站点,但我实际上不希望该页面弹出。

我的意思是变量应该被发送到一些外部站点/页面,但在屏幕上我想被重定向到我的登录页面。但似乎我不知道任何替代方案请指导我。谢谢。

【问题讨论】:

    标签: php


    【解决方案1】:

    您正在搜索PHP cUrl

    $ch = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    // grab URL and pass it to the browser
    curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    

    【讨论】:

    • 是的,几乎就像这样,你能告诉我如何像在标题中一样发送变量('位置:localhost/(some外部站点).php'&?var='test')我可以设置var中的一些值
    【解决方案2】:

    将位置标头设置为您实际要将浏览器重定向到的位置,并使用cURL 之类的内容向远程站点发出 HTTP 请求。

    【讨论】:

      【解决方案3】:

      您通常这样做的方式是通过 cURL 发送这些参数,解析返回值并根据需要使用它们。

      通过使用 cURL,您可以将 POST 和 GET 变量传递给任何 URL。 像这样:

      $ch = curl_init('http://example.org/?aVariable=theValue');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
      

      现在,在 $result 中,您将来自 URL 的响应传递给 curl_init()。

      如果需要发布数据,代码需要多一点:

      $ch = curl_init('http://example.org/page_to_post_to.php');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
      $result = curl_exec($ch);
      curl_close($ch);
      

      同样,您的 POST 请求的结果将保存到 $result。

      【讨论】:

        【解决方案4】:

        您可以通过多种方式在后台连接到另一个 URL。有 cURL(http://php.net/curl - 在之前的 cmets 中已经提到过),有 fopen(http://php.net/manual/en/function.fopen.php),还有 fsockopen(http://php.net/manual/en/function.fsockopen.php - 更高级)

        【讨论】:

          猜你喜欢
          • 2021-03-02
          • 1970-01-01
          • 1970-01-01
          • 2010-10-03
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多