PHP JQuery – Trick Ajax external site (cURL)

//Here is a way to call through Ajax an external link //we use curl in a PHP function to read external url and print result in a Dialog Box //example.html <!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>PHP JQuery - Trick Ajax external site</title> <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> <script type="text/javascript"> function getWrapper() { var request = $.ajax({ url: "wrapper.php", data: { }, dataType: "html", beforeSend: function() { }, success: function(data){ var tag = $("<div></div>"); //This tag will the hold the dialog content. tag.html(data).dialog({ modal: true, title: 'Ajax', width: 'auto', show: 'blind', hide: 'blind' }).dialog('open'); }, complete: function() { }, error: function() { alert('Error'); }, }); } $(document).ready(function() { $('#cmd').click( function() { getWrapper(); }); }); </script> </head> <body> <button id="cmd" name="cmd" >Open</button> </body> //wrapper.php $url = "http://www.google.com"; echo readHTML($url); function readHTML($url) { if(!function_exists("curl_init")) return "cURL extension is not installed"; if (trim($url) == "") die("@ERROR@"); $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERPWD, 'username:password'); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); $response = curl_exec($curl); $resultStatus = curl_getinfo($curl); if($resultStatus['http_code'] == 200) { // All Ok } else { echo 'Call Failed '.print_r($resultStatus); } $curl = null; return utf8_encode($response); } //* readHTML */

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.