开发者

php curl follow redirects?

I have a page that is mea开发者_运维知识库nt to receive a curl request from another server.

I need to format this curl request to another format.

so I have this code

<?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");
?>

so, if someone was to launch a curl request to

http://example.com/intercept/test.php?id=123&from=me&body=something;

Will that call

http://example.com/action/123/me/something/null?

Or if not is there a way i can get it to?

The other one is.

Is there a way I can do this in .htaccess? So I dont have to create a seperate file for this?


Curl doesn't follow redirects by default.

If you're running curl from the command line, you need to add the -L flag to your command to make it follow redirects.

If you're calling curl via a library, you need to set the FOLLOWLOCATION curl option to true (or 1), and the exact code to do that will depend on the language/library/wrapper you're using.


Firstly, I think your code has some issues as you are setting those variables to the result of isset(), which is true or false. Also, setting null for the string when you are including it later is a bad plan, if you want the word null to appear use 'null', if nothing then use the empty string.

That should be:

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

The Location header will tell curl to redirect, which curl will do if given the -L option when called. Note that Location does not support relative URLs, you need to specify the full URL I believe.

Yes, you can do that using mod_rewrite in the file /intercept/.htaccess providing the query string is in the right order and all values are present, it is possible to handle with random order or missing entries, but more complex.

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]

If on the same site, you can use the [L] otherwise specify the full URL and use [R] instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜