반응형
curl을 사용하여 PHP에서 HTTP 코드 가져 오기
CURL을 사용하여 사이트가 작동 / 중지되거나 다른 사이트로 리디렉션되는 경우 상태를 가져옵니다. 가능한 한 간소화하고 싶지만 제대로 작동하지 않습니다.
<?php
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
?>
나는 이것을 함수로 감쌌다. 잘 작동하지만 전체 페이지를 다운로드하기 때문에 성능이 가장 좋지 않습니다. 제거 $output = curl_exec($ch);
하면 0
항상 반환 됩니다.
누구든지 성능을 향상시키는 방법을 알고 있습니까?
먼저 URL이 실제로 유효한지 확인하십시오 (문자열, 비어 있지 않은, 좋은 구문). 이것은 서버 측을 빠르게 확인하는 것입니다. 예를 들어,이 작업을 먼저 수행하면 많은 시간을 절약 할 수 있습니다.
if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){
return false;
}
본문 내용이 아닌 헤더 만 가져와야합니다.
@curl_setopt($ch, CURLOPT_HEADER , true); // we want headers
@curl_setopt($ch, CURLOPT_NOBODY , true); // we don't need body
URL 상태 http 코드를 얻는 방법에 대한 자세한 내용은 내가 작성한 다른 게시물을 참조하십시오 (다음 리디렉션에도 도움이 됨).
전체적으로:
$url = 'http://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo 'HTTP code: ' . $httpcode;
// must set $url first....
$http = curl_init($url);
// do your curl thing here
$result = curl_exec($http);
$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
curl_close($http);
echo $http_status;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$rt = curl_exec($ch);
$info = curl_getinfo($ch);
echo $info["http_code"];
PHP의 " get_headers "기능을 사용해보십시오 .
다음과 같은 내용이 있습니다.
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
curl_getinfo
— 특정 전송에 관한 정보를 얻습니다
curl_getinfo 확인
<?php
// Create a curl handle
$ch = curl_init('http://www.yahoo.com/');
// Execute
curl_exec($ch);
// Check if any error occurred
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
// Close handle
curl_close($ch);
curl_exec
필수적이다. 시도 CURLOPT_NOBODY
몸을 다운로드 할 수 있습니다. 더 빠를 수도 있습니다.
내 솔루션은 정기적으로 서버 상태를 확인하기 위해 Status Http를 가져와야합니다.
$url = 'http://www.example.com'; // Your server link
while(true) {
$strHeader = get_headers($url)[0];
$statusCode = substr($strHeader, 9, 3 );
if($statusCode != 200 ) {
echo 'Server down.';
// Send email
}
else {
echo 'oK';
}
sleep(30);
}
참고 URL : https://stackoverflow.com/questions/11797680/getting-http-code-in-php-using-curl
반응형
'IT' 카테고리의 다른 글
이진 파일을 비교하여 동일한 지 확인하는 방법은 무엇입니까? (0) | 2020.06.08 |
---|---|
줄 바꿈이 파일의 마지막 문자 인 경우 어떻게 삭제합니까? (0) | 2020.06.08 |
MySQL에서 현재 시간에 2 시간을 추가 하시겠습니까? (0) | 2020.06.08 |
임대 Blob이 포함 된 Azure 저장소 계정을 어떻게 삭제합니까? (0) | 2020.06.08 |
여러 개의 인수가있는 Angular 2 파이프를 어떻게 호출합니까? (0) | 2020.06.08 |