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

php curl遵循重定向?

  •  1
  • Hailwood  · 技术社区  · 15 年前

    我有一个页面打算从另一个服务器接收curl请求。

    我需要将这个curl请求格式化为另一种格式。

    所以我有这个密码

    <?php
    $id =     (isset($_GET['msgid'])    ? $_GET['msgid']   : 'null');
    $from =   (isset($_GET['from'])     ? $_GET['from']    : 'null');
    $body =   (isset($_GET['content'])  ? $_GET['content'] : 'null');
    $status = (isset($_GET['status'])   ? $_GET['status']  : 'null');
    
    header("location: ../action/receive_message/$id/$from/$body/$status");
    ?>
    

    所以,如果有人向
    http://example.com/intercept/test.php?id=123&from=me&body=something ;

    会打电话吗
    http://example.com/action/123/me/something/null ?

    或者如果没有,我有办法得到它吗?

    另一个是。

    我有办法做到这一点吗?htaccess? 所以我不必为此创建单独的文件?

    2 回复  |  直到 15 年前
        1
  •  1
  •   grahamparks    15 年前

    默认情况下,Curl不遵循重定向。

    如果从命令行运行curl,则需要添加 -L 标记到您的命令以使其遵循重定向。

    如果通过库调用curl,则需要设置 FOLLOWLOCATION curl选项设置为true(或1),具体的代码将取决于所使用的语言/库/包装器。

        2
  •  0
  •   Orbling    15 年前

    首先,我认为您的代码有一些问题,因为您正在将这些变量设置为 isset() ,这是对还是错。另外,如果您想让null这个词出现,那么在以后包含它时为字符串设置null是一个错误的计划 'null' ,如果没有,则使用空字符串。

    应该是:

    $id =     (isset($_GET['msgid'])    ? $_GET['msgid']   : '');
    $from =   (isset($_GET['from'])     ? $_GET['from']    : '');
    $body =   (isset($_GET['content'])  ? $_GET['content'] : '');
    $status = (isset($_GET['status'])   ? $_GET['status']  : '');
    

    这个 Location header将告诉curl重定向,如果给定 -L 调用时的选项。请注意 位置 不支持相对URL,您需要指定我认为的完整URL。

    是的,你可以用 mod_rewrite 在文件中 /intercept/.htaccess 如果查询字符串的顺序正确且所有值都存在,则可以处理随机顺序或丢失的条目,但更为复杂。

    RewriteEngine on
    RewriteBase /intercept/
    
    # Note, & may need escaping, can not recall
    RewriteCond %{QUERY_STRING} ^id=([^&]+)&from=([^&]+)&content=([^&]+)&status=([^&]+)$
    RewriteRule test.php /action/%1/%2/%3/%4 [L]
    

    如果在同一个站点上,您可以使用 [L] 否则指定完整的URL并使用 [R] 相反。

    推荐文章