Jump to content

Trying to validate if Facebook user exist or not from a .txt file with cURL


WinterSummer

Recommended Posts

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   

Link to comment
Share on other sites

This worked very good, I love you! :D

 

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";
}
Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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!

 

359fb6a2167526e35dd57bcbda78fbbc.png

Edited by WinterSummer
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.