PHP-JSON:检查断开的链接 [英] PHP-JSON : Check broken links

查看:161
本文介绍了PHP-JSON:检查断开的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次尝试使用JSON。
这是我的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 - >找不到,$ retcode = 200,找到。
if($ retcode == 400){
returnfalse;
} else {
returntrue;
}
curl_close($ ch);
}
$ response = array(
'location'=> $ location,
'status'=> $ status
);
$ rr = url_exists($ response ['location']);
echo json_encode($ rr);

JS部分:

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

似乎php页面不会返回结果到json查询。 / p>

编辑:请注意,我忘记安装curl并在我的服务器中启用它。

解决方案

确定,进行测试和试用8小时后,我终于得到这个工作。非常感谢 Vytautas 。他教我很多。主要是如何调试。



对于使用JSON + PHP + CURL检查损坏链接的任何人:


  1. 首先,检查您是否在服务器中安装并启用了 curl

  2. 不理解curl的用户:如果有响应你的网址,会有一个状态码(如200或404)。如果输入的网址为空白,无效或类似内容,则会返回状态代码0

  3. 如果无法从php页面获得正确的响应,请使用FireBug检查标题和响应。还可以使用断点来查看变量是否正确传递。

这里是php代码:

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

if(curl_exec($ ch)=== false)//这里的2行用于调试。
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)

我在php中做了两件事。我应该使用 $ _ GET ['location'] 而不是 $ location ,另一个是 $ response



而且js函数:

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

我在js中做错了另一件事是传递回函数。我应该使用 callback.call 而不是 callback.apply



简单用法:

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


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.

解决方案

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');
      }); 

这篇关于PHP-JSON:检查断开的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆