php.ajax.coder Posted March 14, 2009 Share Posted March 14, 2009 I am using the following function to fetch a web pages source code but there is a problem when a page has a browser detection script is there any way to make the server think that it is a browser and not a script function getSource($url){ //@ this removes the warning messages $source = @file_get_contents($url); if (empty($source)){ return "No Source"; }else{ return $source; } } Thanks Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/ Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 You could try something like: <?php header("User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030516 Ubuntu/9.04 (jaunty) Firefox/3.0.7"); ?> Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-784753 Share on other sites More sharing options...
xylex Posted March 14, 2009 Share Posted March 14, 2009 You need to use curl instead of file_get_contents, and add the headers like curl_set_opt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030516 Ubuntu/9.04 (jaunty) Firefox/3.0.7'); Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-784828 Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 You need to use curl instead of file_get_contents, and add the headers like curl_set_opt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030516 Ubuntu/9.04 (jaunty) Firefox/3.0.7'); Actually, when you think about it, that makes a lot more sense - My suggestion won't work. Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-784883 Share on other sites More sharing options...
php.ajax.coder Posted March 15, 2009 Author Share Posted March 15, 2009 Could you explain how I use this in my function ??? Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-784953 Share on other sites More sharing options...
zq29 Posted March 15, 2009 Share Posted March 15, 2009 Have you checked the manual? cURL and PHP Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-785133 Share on other sites More sharing options...
thebadbad Posted March 15, 2009 Share Posted March 15, 2009 You don't need to use cURL, you can set the user agent with ini_set(): <?php ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'); function getSource($url){ //@ this removes the warning messages $source = @file_get_contents($url); if (empty($source)){ return "No Source"; }else{ return $source; } } ?> Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-785142 Share on other sites More sharing options...
blogfisher Posted March 15, 2009 Share Posted March 15, 2009 I think CURL will help you. I used below function to fetch source code of a webpage : function read_web_page($url) { $ch = curl_init($url); $fp = fopen("d:/test.txt","w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); } pass url of any webpage and it wil save source code to test.txt in D drive. Change it as per your requirement, Link to comment https://forums.phpfreaks.com/topic/149411-browser-detection/#findComment-785150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.