嗨,我知道它是StackOverFlow上一个非常常见的主题。我已经花了整整一周的时间来搜索它。
我有一个网址:http://bayja.com/forum/index.php
我发帖后的这个网址:http://bayja.com/forum/index.php?topic=3454.0
我希望得到:http://bayja.com/forum/index.php?topic=3454.0发帖后获取主题编号" 3454.0"
这是我的代码:
function post($subj, $mess, $user, $password, $board, $domain) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_USERAGENT, "Automatic SMF poster thing");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=".$user."&passwrd=".$password);
curl_setopt($ch, CURLOPT_URL, "$domain?action=login2");
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "$domain?action=post;board=".$board.".0");
$data = curl_exec($ch);
sleep(3);
preg_match ( "<input type=\"hidden\" name=\"sc\" value=\"(.*)\">", $data, $sc);
preg_match ( "<input type=\"hidden\" name=\"seqnum\" value=\"(.*)\">", $data, $seqnum);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "subject=".urlencode($subj)."&icon=xx&message=".urlencode($mess)."¬ify=0&lock=0&goback=1&sticky=0&move=0&attachment%5B%5D=&attachmentPreview=&post=xxxxx&sc=".$sc[1]."&seqnum=4&seqnum=".$seqnum[1]);
curl_setopt($ch, CURLOPT_URL, "$domain?action=post2;start=0;board=".$board);
curl_exec($ch);
curl_close($ch);
}
请帮助我在帖子后获取主题号码获取网址。 非常感谢你。
答案 0 :(得分:1)
使用curl_getinfo与CURLINFO_EFFECTIVE_URL
一起获取最后一个网址,regex
与主题匹配,即:
...
curl_exec($ch);
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$topic = preg_replace('/.*topic=(.*?)$/m', '$1', $lastUrl);
echo $topic;
//3454.0
curl_close($ch);
<强>更新强>
你可以帮我讲一下preg_match 吗?
preg_match_all('/.*topic=(.*?)$/m', $lastUrl, $topic, PREG_PATTERN_ORDER);
$topic = $topic[1][0];