1

我有一个在线项目“本周问题”;这个项目允许用户提交他们的问题。这些问题保存在一个mysql表中;问题也被发送到另一个simplepost.php,该文件将问题存储在phpBB 中。我想对每个问题都使用这个 phpBB 来讨论这个问题。

所以现在,我的项目将问题存储在 sql 表中,并在 phpBB 中发布问题。

但是当问题发布在 phpBB 中时,它会存储在“ http://servername.com/phpBB3/viewtopic.php?f=5&t=24 ”中,其中 t=24 是问题。

我想以某种方式获取这个 url 并提取 t=24,这样我就可以为我的项目中的每个问题提供一个可点击的链接,以指导其特定 phpBB 页面的问题。

假设我的项目在:http ://servername.com/qotw/profile.html (这允许用户发布问题并将问题插入到 sql 表中,并在 phpBB/simplepost.php 中调用将问题发布到phpBB)

可以在 php 中看到这个问题:“ http://servername.com/phpBB3/viewtopic.php?f=5&t=24

请向我建议我该怎么做。我怎样才能从这个网址中获取这个“t = 24”。

当我的 simplepost.php 被调用时,是使用 posting.php 发布问题并返回一个返回值。

simplepost.php 中的代码如下所示:

$title = "This is the title of the message.";
//$body = "This is the message body.";
$post_fields = array(
             'subject'   => $title,
             'addbbcode20' => 100,
             'message' => $body,
             'lastclick'          => $lclick[0],
             'post'   => 'Submit',
             'attach_sig'        => 'on',
             'creation_time'      => $lclick[0],
             'form_token'   => $security123[1],
             'filecomment' => '',
             );

//Wait (you might also do this by setting lastclick in the past by 3 seconds
sleep(3);

//Set up curl session for posting the message
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$purl);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch1, CURLOPT_HEADER, false );
curl_setopt($ch1, CURLOPT_COOKIE,'cookie.txt');
curl_setopt($ch1, CURLOPT_COOKIEJAR,'cookie.txt');
curl_setopt($ch1, CURLOPT_COOKIEFILE,'cookie.txt');
$result2= curl_exec ($ch1);
//$result3= curl_exec ($ch1, CURLOPT_URL,$purl);
curl_close ($ch1);

echo $result2;

响应来自 $result2。页面转到http://servername.com/phpBB3/viewtopic.php?f=5&t=24 ”。

但问题是这一切都发生在后端。我的项目没有显示 phpBB 的 viewtopic.php 页面。

4

4 回答 4

2

如果我没记错的话,你应该可以使用 PHP 来获取存储在 URL 中的 't' 变量中的 'value' (24),使用$_GET['t']. 不过,这仅在您当前位于该 URL 时才有效。见:http ://us2.php.net/manual/en/reserved.variables.get.php

如果您不在该特定页面上时尝试仅抓取该部分,则可以尝试:

ereg("t=[0-9]+", $url, $res)

然后从 $res 数组的结果中去掉 "t="

于 2009-07-24T19:48:21.927 回答
1

要从 URL 中提取组件,您可以使用parse_url-- 好吧,如果您只想获取查询字符串,$_SERVER['QUERY_STRING']也可以。

然后,要从查询字符串中获取参数/值,您可以查看parse_str:有一个示例可以显示您想要的内容:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>

显然,您可能更喜欢第二种方式,以避免在脚本中注入(未知和潜在危险的,例如register_globals变量。

于 2009-07-24T19:37:49.093 回答
1

不太确定你想要什么,但我可能是愚蠢的。

给定一个网址,$url您可以这样做:

$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
$t = $params['t'];

如果页面传递了 GET 参数,您可以执行以下操作:

$t = $_GET['t'];
于 2009-07-24T19:40:15.313 回答
0

怎么样:

$var = $_GET['t']; 

过滤它并用它做任何事情。

于 2009-07-24T19:51:56.037 回答