1

First time trying to use JSON. Here is my checklink.php :

function url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // $retcode > 400 -> not found, $retcode = 200, found.
    if ($retcode == 400){
    return "false";
    }else{
    return "true";
    }
    curl_close($ch);
}
$response = array( 
  'location' => $location, 
  'status' => $status 
);
$rr = url_exists($response['location']);
echo json_encode( $rr );

JS part :

function UrlExistsNew(url, callback) {
  $.getJSON('checklink.php', { location: url }, function ( data ) {
  callback.apply( null, data.status );
});
}
...
UrlExistsNew($(this).val(), function(status){
        if(status === "false") $(element).css('background-color','#FC0');
      }); 
...

It seems the php page is not returning result to json query.

Edit : Note that I forgot to install curl and enable it in my server. I Hope no one miss this.

9
  • 1
    you are not returning any result at all except true or false? where is the json? Commented May 3, 2012 at 7:23
  • @thecodeparadox I don't understand what you trying to ask. I have no idea what to do on both php and json side. What you mean where is the json ? Commented May 3, 2012 at 7:27
  • Try some debbugging tools like firebug or chrome developers tool.. Commented May 3, 2012 at 7:37
  • @Vytautas I am using firebug to mark breakpoints and see if js is getting callback result from php or not. Commented May 3, 2012 at 7:38
  • 1
    so post your response.. becouse both answers should fix your issue unles there is something else.. go to console tab and show what you can see there. Commented May 3, 2012 at 7:44

3 Answers 3

1

You should change $rr = url_exists($response['location']); to

$rr = array("status"=>url_exists($response['location']));

to get the json response as you expect

Sign up to request clarification or add additional context in comments.

Comments

1

OK, After doing test and trials for 8 hours. I finally got this working. Thanks a lot to Vytautas. He teached me a lot. Mostly how to debug.

For anyone who wants to check broken links using JSON + PHP + CURL :

  1. First of all, Check if you have curl installed and enabled in your server.
  2. Those who don't understand curl : If there is a response from your url, there will be a status code (like 200 or 404). If the url entered is blank, invalid or anything like that, It will return status code 0
  3. If you can't get the proper response from php page, use FireBug (Console Tab) to check the headers and responses. Also use breakpoint to see if variables are passing correctly.

Here is the php code :

function url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    if(curl_exec($ch) === false) // These 2 line here are for debugging.
        die('Curl error: ' . curl_error($ch));

    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $retcode;
}
$response = array(
  'status' => url_exists($_GET['location'])
);
echo json_encode($response)

I was doing 2 thing wrong in php. I should have used $_GET['location'] instead of $location And the other was $response instead of using a second variable.

And the js function :

function UrlExistsNew(url, callback) {
  $.getJSON('checklink.php', { location: url }, function ( data ) {
  callback.call( null, data.status );
});
}

Another thing I was doing wrong in js was passing callback to the function. I should have used callback.call instead of callback.apply

Simple usage :

UrlExistsNew($(this).val(), function(status){
        if(status === 404) $(element).css('background-color','#FC0');
      }); 

Comments

0
$rr = url_exists($response['location']);
echo json_encode( array('status' => $rr) );

And try this:

UrlExistsNew($(this).val(), function(status){
   if(!status) $(element).css('background-color','#FC0');
}); 

1 Comment

Tried your update. Still not working. I think something is missing. When I put breakpoint at $.getJSON('checklink.php', { location: url }, function ( data ) { It does stop but it doesn't at callback.apply( null, data.status ); line

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.