0

I just want to ask... how can i convert this code:

$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
    // chat user
        $ch1 = curl_init();
        curl_setopt($ch1, CURLOPT_URL, $url);
        curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
        $output1 = curl_exec($ch1);
        $info1 = curl_getinfo($ch1);
        curl_close($ch1);
        $userinfo = json_decode($output1);

to ajax... I want to call the json data automatically... hope you can help mo on this.. thanks..

3
  • 1
    where is the js code, what did you try ? Commented Jul 20, 2017 at 6:31
  • i think you want this code load automatic? if yes please put this code in a file and call that file using ajax in a particular time interval. Commented Jul 20, 2017 at 6:33
  • @madalinivascu the json is on the link Commented Jul 20, 2017 at 7:30

1 Answer 1

2

You can use $.ajax or $.post to achive this. Below is an example

in PHP script

<?php
$url='https://www.zopim.com/api/v2/agents';
$username="boom";
$password="baam";
?>

In javacript

<script type="text/javascript">
    // Using the core $.ajax() method
    $.ajax({

        // The URL for the request
        url: "<?php echo $url; ?>",

        // The data to send (will be converted to a query string)
        data: {
            username: "<?php echo $username; ?>",
            password: "<?php echo $password; ?>"
        },

        // Whether this is a POST or GET request
        type: "POST",

        // The type of data we expect back
        dataType : "json",
    })
      // Code to run if the request succeeds (is done);
      // The response is passed to the function
      .done(function( json ) {
         //Hurray!! here is your json data
         console.log(json.id);
         console.log(json.title);

      })
      // Code to run if the request fails; the raw request and
      // status codes are passed to the function
      .fail(function( xhr, status, errorThrown ) {
        alert( "Sorry, there was a problem!" );
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
      })
      // Code to run regardless of success or failure;
      .always(function( xhr, status ) {
        alert( "The request is complete!" );
      });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this got but i got an error... XMLHttpRequest cannot load https://www.zopim.com/api/v2/agents. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.