Get Content of HMTL Page Using PHP CURL

Posted by & filed under Ajax, JQUERY, PHP.

In some situation you may need to get html content of given URL. Here is I am showing one of the method to get html content of given URL using PHP CURL.

You may use file_get_contents()  method in PHP to get html content of given URL, but most of the sever restricted access to the html page if you are going to access page using file_get_contents(). So it is safer to use PHP CURL to access to given HTML webpage  content.

Even you may access HTML content of the https page using PHP CURL.

 

 

Get Content of HMTL Page Using PHP CURL

Get Content of HMTL Page Using PHP CURL

 

Step 1:

Create HTML page with HTML Form with text box to get website URL from users. When users enters valid website URL, I am posting  that given URL to  server (PHP FILE) using jQuery ajax. Where I am sending  request to server particular server to get content HTML page Using PHP CURL.

Here is the HTML page.

<!DOCTYPE html> 
<html>
<head>
	<title>Get Content of HMTL Page Using PHP CURL</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<meta name="description" content="using php curl to get data, php curl get contents of page, Get html source code with cURL, Get HTML Code of HTML File using PHP CURL, Get content of page using PHP cURL, How to get a web page using CURL, how to extract data from a website using php, php curl https get example, php script to extract data from website">
	<meta name="keywords" content="using php curl to get data, php curl get contents of page, Get html source code with cURL, Get HTML Code of HTML File using PHP CURL, Get content of page using PHP cURL, How to get a web page using CURL, how to extract data from a website using php, php curl https get example, php script to extract data from website">

	<link rel="stylesheet" href="css/bootstrap.min.css" />
	<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" href="css/style.css" >
</head>
<body>
	<div class="container">
		<h1 class="text-center page-title" >Get Content of HMTL Page Using PHP CURL</h1>
		<div class="row url-getter">
			<div class="col-md-offset-2 col-md-8">
				<form role="form" id="siteform" method="post">
					<div class="form-group">
						<input type="url" class="form-control" name="site_url" id="site_url" placeholder="Enter your site address">
						<span class="help-block"></span>
					</div>
					<button data-loading-text="Please wait..." type="submit" id="url_getter" class="btn btn-default btn-success">Submit</button>
				</form>
			</div>
		</div>
		<div class="clearfix"></div>
		<div class="row">
			<div class="col-md-12">
				<h1 class="text-center page-title" >New Site</h1>
				<iframe style="border: 0 none !important;height: 1000px;width: 100%;" id="site"></iframe>		
			</div>
		</div>

	</div>

	<script src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
	<script src="js/jquery.validate.min.js"></script>
    <script src="js/script.js"></script>
</body>
</html>

 

 

Step 2:

Here PHP CURL script to get HTML content given URL.

<?php

if( isset( $_POST['site_url'] ) && !empty( $_POST['site_url'] ) ){
	echo get_html($_POST['site_url']);
} else {
	echo 'false';
}

function get_html($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

?>

Step 3:

Here is the jQuery script for validation and ajax request. Once we get response from the server (i.e. PHP), Using jQuery I am assigning that response to IFRAME in that page.

$(document).ready(function(){
	$("#siteform").validate({
		submitHandler : function(e) {
		    $('#url_getter').button('loading');
			site_url= $('#site_url').val();
	        $.ajax({
	            url: 'ajax.php',
	            method: 'post',
	            data: { site_url: site_url },
	            success: function (data) {
	            	$('#url_getter').button('reset');
	                if(data){
	                	$('#site').contents().find("body").html( data);
	                } else {
	                	alert('Something went wrong please try again');
	                }

	            },
	            cache: false
	        });
		},
		rules : {
			site_url : {
				required : true,
				url: true
			}	
		},
		messages : {
			site_url : {
				required : "Please enter your site address",
			}
		},
		errorPlacement : function(error, element) {
			$(element).closest('.form-group').find('.help-block').html(error.html());
		},
		highlight : function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
		},
		success : function(element, lab) {
			var messages = new Array("That's Great!", "Looks good!", "You got it!", "Great Job!", "Smart!", "That's it!");
			var num = Math.floor(Math.random() * 6);
			$(lab).closest('.form-group').find('.help-block').text(messages[num]);
			$(lab).addClass('valid').closest('.form-group').removeClass('has-error').addClass('has-success');
		}
	});

});

 

 

 .

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]gmail.com

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

%d bloggers like this:

Get Instant Script Download Access!