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


<!doctype html>
 <html lang="en">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>PHP JQuery - Trick Ajax external site</title>

<link rel="stylesheet" href="" type="text/css" media="all" />

 <script src="//"></script>
 <script src="//"></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.
                            modal: true, 
                            title: 'Ajax', 
                            width: 'auto',
                            show: 'blind',
                            hide: 'blind'
    complete:   function() {
    error: function() {
 $(document).ready(function() {
   $('#cmd').click( function() {
<button id="cmd" name="cmd" >Open</button>


$url = "";
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: Logo

You are commenting using your 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