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 */
This entry was posted in JQuery, PHP and tagged . Bookmark the permalink.

2 Responses to PHP JQuery – Trick Ajax external site (cURL)

  1. After looking at a few of the blog posts on your site, I
    really appreciate your way of blogging. I
    book-marked it to my bookmark website list and will
    be checking back in the near future. Take a look at my website as well and
    tell me what you think.

  2. Azad says:

    Thank you very much. code is working perfectly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s