안녕하세요, curl을 사용하여 웹 외부 서버와 통신하지만 응답 유형은 html이므로 json 코드 (4000 행 이상)로 변환 할 수 있었지만 결과가 포함 된 특정 행을 얻는 방법을 모릅니다. 어떤 생각?
내 cUrl 코드는 다음과 같습니다.
require_once('getJson.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.reputationauthority.org/domain_lookup.php?ip=website.com&Submit.x=9&Submit.y=5&Submit=Search');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = '<<<EOF'.$data.'EOF';
$json = new GetJson();
header("Content-Type: text/plain");
$res = json_encode($json->html_to_obj($data), JSON_PRETTY_PRINT);
$myArray = json_decode($res,true);
getJson.php의 경우
class GetJson{
function html_to_obj($html) {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
return $this->element_to_obj($dom->documentElement);
}
function element_to_obj($element) {
if ($element->nodeType == XML_ELEMENT_NODE){
$obj = array( "tag" => $element->tagName );
foreach ($element->attributes as $attribute) {
$obj[$attribute->name] = $attribute->value;
}
foreach ($element->childNodes as $subElement) {
if ($subElement->nodeType == XML_TEXT_NODE) {
$obj["html"] = $subElement->wholeText;
}
else {
$obj["children"][] = $this->element_to_obj($subElement);
}
}
return $obj;
}
}
}
내 아이디어는 lign 2175를 달성하기 위해 행 찾아보기 대신에 (예 : $ data [ 'children'] [2] [ 'children'] [7] [ 'children'] [3] [ 'children'] [1] [ 'children'] [1] [ 'children'] [0] [ 'children'] [1] [ 'children'] [0] [ 'children'] [1] [ 'children'] [2] [ ' children '] [0] ['children '] [0] ['html ']은 나에게 좋은 생각이 아닙니다.) 직접 이동하고 싶습니다.
반환되는 HTML이 매번 일관된 구조를 갖고 있고 그 일부에서 특정 값을 하나만 원하는 경우 정규식을 사용하여 HTML을 구문 분석하고 필요한 부분을 찾을 수 있습니다. 이것은 모든 것을 배열에 넣으려는 대안입니다. HTML 문서를 구문 분석하고 특정 항목을 찾기 위해 이전에이 기술을 사용했습니다. 여기에 간단한 예가 있습니다. 찾고있는 데이터의 정확한 특성을 지정하지 않았기 때문에 필요에 맞게 조정해야합니다. 올바른 비트를 찾으려면 여러 수준의 구문 분석을 내려야 할 수 있습니다.
$data = curl_exec($ch);
//Split the output into an array that we can loop through line by line
$array = preg_split('/\n/',$data);
//For each line in the output
foreach ($array as $element)
{
//See if the line contains a hyperlink
if (preg_match("/<a href/", "$element"))
{
...[do something here, e.g. store the data retrieved, or do more matching to find something within it]...
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다