2012-05-03 2 views
1

JSON을 처음 사용하려고 시도했습니다.PHP-JSON : 깨어진 링크 확인

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 부분 :

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

PHP 페이지는 JSON 쿼리에 결과를 반환하지 않습니다 보인다 여기 내 checklink.php입니다.

편집 : curl을 설치하고 내 서버에서 사용하도록 설정하는 것을 잊어 버렸습니다. 나는 아무도 이것을 놓치지 않기를 바란다.

+1

당신은 참 또는 거짓을 제외하고 전혀 결과를 반환되지 않습니다 내가 대신 callback.apply

간단한 사용의 callback.call을 사용해야? 아들은 어디 있니? – thecodeparadox

+0

@thecodeparadox 나는 당신이 묻고 자하는 것을 이해하지 못합니다. php와 json 측 모두에서 무엇을해야할지 모르겠습니다. 아들이 어디 있니? – xperator

+0

방화 광 또는 크롬 개발자 도구와 같은 디버깅 도구를 사용해보세요. – Vytautas

답변

1

확인 시험을 수행 한 후 8 시간 동안 시련. 나는이 일을 마침내 얻었다. Vytautas에게 감사드립니다. 그는 저를 많이 가르쳤습니다. 주로 디버깅하는 방법. JSON + PHP + 컬을 사용하여 깨진 링크를 확인하고자하는 사람들을위한

: 당신이 설치하고 서버에서 사용할 수있는 경우 모든

  1. 먼저 확인합니다.
  2. 말풍통을 이해하지 못하는 사용자 : URL의 응답이있는 경우 상태 코드 (예 : 200 또는 404)가 표시됩니다. 입력 된 URL이 비어 있거나 잘못되었거나 그와 같은 것이 있으면 상태 코드 0을 반환합니다.
  3. PHP 페이지에서 적절한 응답을 얻을 수 없다면 FireBug (Console Tab)를 사용하여 헤더 및 응답을 확인하십시오. 또한 변수가 올바르게 전달되는지 보려면 중단 점을 사용하십시오.

    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) 
    

    내가 PHP에서 잘못이 일을했다 : 여기

은 PHP 코드입니다. $location 대신 $_GET['location']을 사용해야했고 다른 하나는 두 번째 변수를 사용하는 대신 $response이었습니다.

그리고 JS 기능 :

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

또 다른 것은 내가 JS 잘못하고 있던 함수에 콜백을 전달했다.

UrlExistsNew($(this).val(), function(status){ 
     if(status === 404) $(element).css('background-color','#FC0'); 
     }); 
0
$rr = url_exists($response['location']); 
echo json_encode(array('status' => $rr)); 

을 예상대로 JSON 응답을 얻을 $rr = url_exists($response['location']);

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

로 변경하고 시도해야이 :

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

아직 작동하지 않습니다. – xperator

+0

업데이트를 시도했습니다. 그래도 작동이 안되는. 나는 뭔가 빠져 있다고 생각한다. 중단 점을'$ .getJSON ('checklink.php', {location : url}, function (data) {'멈추지 만'callback.apply (null, data.status); 선 – xperator