繁体   English   中英

PHP的卷曲跟随重定向?

[英]php curl follow redirects?

我有一个旨在接收来自其他服务器的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中做到这一点吗? 因此,我不必为此创建单独的文件吗?

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

如果从命令行运行curl,则需要在命令中添加-L标志,以使其遵循重定向。

如果要通过库调用curl,则需要将FOLLOWLOCATION curl选项设置为true(或1),执行此操作的确切代码取决于您使用的语言/库/包装器。

首先,我认为您的代码存在一些问题,因为您正在将这些变量设置为isset()的结果,即true或false。 另外,在以后添加该字符串时将其设置为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标头将告诉curl重定向,如果在调用时给了-L选项,curl将执行重定向。 请注意, Location不支持相对URL,我需要指定完整的URL。

是的,您可以在文件/intercept/.htaccess中使用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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM