Posts Tagged ‘tutorial’

Insert Your Recent Comments from Facebook to Database

The sample is for scraping data from Facebook home page. Once you will pass your username and password in variables  i have declared in file and execute the code you will get your home page of Facebook.

Here php code will pass the variables by POST method to page and login into facebook account then other curl call will call my home page to get the home page HTML.

I have written code of how i am scrapping data by DOM Parser.

$first_name = 'First Name';
$login_email = 'facebookloginemail';
$login_pass = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070309 Firefox/");

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, '');
$page = curl_exec($ch);

Here it will print the facebook home page with your recent updates

echo $page;

// Parse the HTML information and return the results.
$dom = new DOMDocument();

$xpath = new DOMXPath($dom);

// Get a list of articles from the section page

$articleList = $xpath->query("*/div[@id='m_stream_stories']");

// Add each articles url to the Articles array
// This way we can scrap any part of data of any sites.

$articles = array();
foreach ($articleList as $item){
	$articles[] = array(
								'url' => $item->getElementsByTagName('a')->item(0)->getAttribute('href')


// Here we can loop for the article array and insert it into database.

If you have any trouble in getting it work let me know i will help you.

Uploading file by PHP Code

Let’s start by creating the html file form:

Files are uploaded from the browser using an input tag, with the type parameter set to “file”. This is supported by all browsers currently available on the market.

The most important thing is to set the ENCTYPE attribute of the form to “multipart/form-data” and set the form’s action to the file upload page. The file upload page will handle the actual file uploading.
html file : upload.html

 <form name="frmupload" id="frmupload" method="post" action="/upload.php" enctype="multipart/form-data">
  <legend>File upload:</legend>
	  <label for="file">File:</label>
	  <input name="vFile" type="file" id="vFile" />
  <input type="submit" name="upload" value="Upload" />

Now first create the upload script:
In PHP, uploaded files are accessed via the $_FILES array:

* $_FILES["file"]["name"]: The original filename on the client’s machine.
* $_FILES['file']['type']: The mime type of the file.
* $_FILES['file']['size']: The size, in bytes, of the uploaded file.
* $_FILES['file']['tmp_name']: The temporary filename of the file in which the uploaded file was stored on the server.

Here i have created one function so we will call this in our upload.php it will makes easy to upload , optimize the code and make the code reusable.

php script file : upload.php

$imagepath='';		// Make Sure you have set the permission of folder in which you are uploading files.
$image= $_FILES['file']['tmp_name'];
$image_name= $_FILES['file']['name'];
$prefix=''; 	// Optional
$vaildext= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png"; 

$output = fileupload($imagepath , $image , $image_name , $prefix , $vaildext);

fileupload($imagepath,$image,$image_name,$prefix='', $vaildExt= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png");
 function fileupload($imagepath,$image,$image_name,$prefix='', $vaildext= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png")

	   if(!empty($image_name) and is_file($image) )

			$vaildext_arr = explode(",", strtoupper($vaildext));
			if(in_array(strtoupper($ext), $vaildext_arr))
				$ftppath1 = $imagepath.$imagefile;
				if(!copy($image, $ftppath1))
					$imagefile = '';
					$msg="Failed to Upload files !!";
					$msg="File uploaded successfully !!";
				$imagefile = '';
				$msg="Extension Is Not Valid, please use mentioned extesions only $vaildExt !!!";
		$ret[0] = $imagefile;
		$ret[1] = $msg;
		return $ret;

Its easy and working :)

Let me know your thoughts if you are facing any problem while uploading files.
Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.