sammy
sammy

Reputation: 69

How I get url after posting with Curl

Hi I know its a very common topic on StackOverFlow. I have already spent my entire week to search it out.

I have a url : http://bayja.com/forum/index.php

This url after I post : http://bayja.com/forum/index.php?topic=3454.0

I want to get : http://bayja.com/forum/index.php?topic=3454.0 after post for get topic number "3454.0"

here is my code:

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)."&notify=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);
}

Please help me to get url after post for get topic number. Thank you so much.

Upvotes: 1

Views: 65

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 98991

Use curl_getinfo with CURLINFO_EFFECTIVE_URL to get the last URL and a regex to match the topic i.e.:

...
curl_exec($ch);
$lastUrl =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$topic = preg_replace('/.*topic=(.*?)$/m', '$1', $lastUrl);
echo $topic;  
//3454.0
curl_close($ch);

UPDATE:

can you help me about preg_match

preg_match_all('/.*topic=(.*?)$/m', $lastUrl, $topic, PREG_PATTERN_ORDER);
$topic = $topic[1][0];

Upvotes: 1

Related Questions