WinterSummer Posted August 16, 2015 Share Posted August 16, 2015 I have a .txt files that contains a few facebook usernames and I want to check if those usernames exists, i.e if Facebook returns a real profile or if it returns a 404 page. I want to save all usernames that exists in a .txt file. function get_data($url) { $ch = curl_init(); $timeout = 100; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } curl_close($ch); return $data; } $handle = fopen("usernames.txt", "r"); if ($handle) { while ($username = fgets($handle) !== false) { echo $url = "https://facebook.com/" . $username; $source = get_data($url); if (preg_match_all('/<div class="coverBorder">/', $source, $matches)) // If user exists, print its name ($line). <div class="coverBorder"> confirms that the user exists since this class does not exist on Facebook's 404 page echo $matches; } fclose($handle); } else { echo "Error opening file"; } I get those errors: https://facebook.com/sanna Curl error: Illegal characters found in URLhttps://facebook.com/asodkasopdkaopsasf Curl error: Illegal characters found in URLhttps://facebook.com/jana Curl error: Illegal characters found in URLhttps://facebook.com/henrik usernames.txt looks like: sanna asodkasopdkaopsasf jana henrik Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 16, 2015 Share Posted August 16, 2015 (edited) You are getting that error due the cartridge return and or newline whitespace characters (\r\n or \n or \r) being included when you read the username from each line in the text file. When you get the username you need to use trim Edited August 16, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
WinterSummer Posted August 16, 2015 Author Share Posted August 16, 2015 This worked very good, I love you! Now I want to save all usernames that exists in a .txt file that gets downloaded to my computer when I visit the website. How do I achieve that? if ($handle) { while ($username = fgets($handle) !== false) { $url = "https://facebook.com/" . trim($username); $source = get_data($url); if (preg_match_all('/<div class="coverBorder">/', $source, $matches)) // If user exists, print its name ($line). <div class="coverBorder"> confirms that the user exists since this class does not exist on Facebook's 404 page //Save $username in an array then export it to a .txt file? } fclose($handle); } else { echo "Error opening file"; } Quote Link to comment Share on other sites More sharing options...
WinterSummer Posted August 16, 2015 Author Share Posted August 16, 2015 You are getting that error due the cartridge return and or newline whitespace characters (\r\n or \n or \r) being included when you read the username from each line in the text file. When you get the username you need to use trim I can't edit my last post but I wanted to quote you so you get notified that I would be happy if you could help me with the last bit. Quote Link to comment Share on other sites More sharing options...
WinterSummer Posted August 17, 2015 Author Share Posted August 17, 2015 (edited) I have been working on the code and it currently looks like this: <?php function get_data($url) { $ch = curl_init(); $timeout = 30; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } curl_close($ch); return $data; } $valid_username = array(); $handle = fopen("usernames.txt", "r"); if ($handle) { while (($username = fgets($handle)) !== false) { $url = "facebook.com/" . trim($username); $source = get_data($url); echo $url . "<br />"; if(preg_match_all('/<div class="coverBorder">/', $source, $matches)) { // If user exists, print its name. <div class="coverBorder"> confirms that the user exists since this class does not exist on Facebook's 404 page echo "<font color='green'>" . $username . "</font>"; array_push($valid_username, $username); } else echo "<font color='red'>" . $username . "</font><br />"; echo "<hr />"; } fclose($handle); } else { echo "Error opening file"; } ?> What I don't understand is that it's reporting false on some of the usernames even if though they do exist: lisa and johanna should be green, not red! Edited August 17, 2015 by WinterSummer Quote Link to comment Share on other sites More sharing options...
WinterSummer Posted August 18, 2015 Author Share Posted August 18, 2015 I'm still stuck on this, can't figure out why lisa and johanna are reported as false... Would be very happy if someone could give me a hand. Quote Link to comment 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.