dtdetu Posted December 6, 2008 Share Posted December 6, 2008 hello i want to ask if anyone knows how to get the page name with curl, i mean i use curl to go to a page but that page forwards me to another page, is it possible to get the page name of the last redirection. thanks Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/ Share on other sites More sharing options...
gaza165 Posted December 6, 2008 Share Posted December 6, 2008 some code would be nice, how about using sessions to store the previous url on the redirect?? Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707586 Share on other sites More sharing options...
dtdetu Posted December 6, 2008 Author Share Posted December 6, 2008 how can i do that, can u give an example please Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707589 Share on other sites More sharing options...
gaza165 Posted December 6, 2008 Share Posted December 6, 2008 <?php session_start(); function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } $_SESSION['url'] = curPageURL(); ?> the code above gets the current url for which you are on, then it will store it to a session variable then on redirect do the following code this will be on the page it is redircted to... <?php session_start(); echo $_SESSION['url']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707602 Share on other sites More sharing options...
dtdetu Posted December 6, 2008 Author Share Posted December 6, 2008 but how can i use this with curl my code is this $ch = curl_init("$l[link]"); curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'files/cookie.txt'); $result = curl_exec($ch); this code redirects me to some url how can i get that url Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707609 Share on other sites More sharing options...
gaza165 Posted December 6, 2008 Share Posted December 6, 2008 what url is it redirecting to? is a set url? or just a random one? explain what you are trying to do fully. Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707615 Share on other sites More sharing options...
dtdetu Posted December 6, 2008 Author Share Posted December 6, 2008 it redirects me to a random url which i dont know , i want to get the url like $redirectedurl = $??; Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707621 Share on other sites More sharing options...
gaza165 Posted December 6, 2008 Share Posted December 6, 2008 is the code you gave me the full code for the redirect?? Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707635 Share on other sites More sharing options...
dtdetu Posted December 6, 2008 Author Share Posted December 6, 2008 yes Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707657 Share on other sites More sharing options...
GingerRobot Posted December 6, 2008 Share Posted December 6, 2008 it redirects me to a random url which i dont know , i want to get the url like $redirectedurl = $??; You need to set the CURLOPT_FOLLOWLOCATION option to false and the CURLOPT_RETURNTRANSFER option to true. Then parse the returned data for the location. The method of redirection determines where you need to look. For example, if you're being redirected with an HTTP header, then you'll also need to set the CURLOPT_HEADER option to true and look for the text after 'location' Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707677 Share on other sites More sharing options...
dtdetu Posted December 6, 2008 Author Share Posted December 6, 2008 hey i found the solution in case someone needs <? function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => true, // return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); //$header['errno'] = $err; // $header['errmsg'] = $errmsg; //$header['content'] = $content; print($header[0]); return $header; } $thisurl = "myurl"; $myUrlInfo = get_web_page( $thisurl ); echo $myUrlInfo["url"]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135791-solved-curl-question/#findComment-707679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.