PHP에서 링크 (웹 페이지)의 HTML 코드를 검색하고 싶습니다. 예를 들어, 링크가
그런 다음 제공되는 페이지의 HTML 코드를 원합니다. 이 HTML 코드를 검색하여 PHP 변수에 저장하고 싶습니다.
어떻게 할 수 있습니까?
PHP에서 링크 (웹 페이지)의 HTML 코드를 검색하고 싶습니다. 예를 들어, 링크가
그런 다음 제공되는 페이지의 HTML 코드를 원합니다. 이 HTML 코드를 검색하여 PHP 변수에 저장하고 싶습니다.
어떻게 할 수 있습니까?
답변:
PHP 서버가 url fopen 래퍼를 허용하는 경우 가장 간단한 방법은 다음과 같습니다.
$html = file_get_contents('/programming/ask');
더 많은 제어가 필요하면 cURL 함수를 살펴 봐야 합니다.
$c = curl_init('/programming/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
또한 검색된 페이지를 어떻게 든 조작하려면 PHP DOM 파서를 사용해 볼 수 있습니다. 내가 찾을 수 PHP는 간단한 HTML DOM 파서를 사용하는 것은 매우 쉽습니다.
Yahoo : http://developer.yahoo.com/yql 에서 YQL 라이브러리를 확인할 수 있습니다.
당면한 작업은 다음과 같이 간단합니다.
select * from html where url = 'http://stackoverflow.com/questions/ask'
콘솔 ( http://developer.yahoo.com/yql/console) 에서 시도해 볼 수 있습니다 (로그인 필요).
더 많은 작업을 수행 할 수있는 좋은 아이디어는 Chris Heilmanns screencast를 참조하십시오. http://developer.yahoo.net/blogs/theater/archives/2009/04/screencast_collating_distributed_information.html
간단한 방법 : 사용 file_get_contents():
$page = file_get_contents('http://stackoverflow.com/questions/ask');
참고 allow_url_fopen해야합니다 true당신에이 php.iniURL 인식하면 fopen 래퍼를 사용할 수 있습니다.
더 진보 된 방법 : 당신이 당신의 PHP 구성을 변경할 수없는 경우 allow_url_fopen입니다 false기본적으로하고 내선 / 컬이 설치되어있는 경우, 사용하는 cURL라이브러리를 원하는 페이지로 연결할 수 있습니다.
include_once('simple_html_dom.php');
$url="http://stackoverflow.com/questions/ask";
$html = file_get_html($url);
이 코드를 사용하여 전체 HTML 코드를 배열 (파싱 된 형식)로 얻을 수 있습니다. 여기에서 'simple_html_dom.php'파일을 다운로드하십시오. http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download
다음은 URL에서 콘텐츠를 가져 오는 두 가지 다른 간단한 방법입니다 .
1) 첫 번째 방법
호스팅 (php.ini 또는 다른 곳)에서 Allow_url_include 활성화
<?php
$variableee = readfile("http://example.com/");
echo $variableee;
?>
또는
2) 두 번째 방법
php_curl, php_imap 및 php_openssl 활성화
<?php
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_dataa($url) {
$ch = curl_init();
$timeout = 5;
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_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$variableee = get_dataa('http://example.com');
echo $variableee;
?>
DomDocument 메서드를 사용하여 개별 HTML 태그 수준 변수도 가져올 수 있습니다.
$homepage = file_get_contents('https://www.example.com/');
$doc = new DOMDocument;
$doc->loadHTML($homepage);
$titles = $doc->getElementsByTagName('h3');
echo $titles->item(0)->nodeValue;
나는이 코드를 시도했고 그것은 나를 위해 일하고있다.
$html = file_get_contents('www.google.com');
$myVar = htmlspecialchars($html, ENT_QUOTES);
echo($myVar);