2016-06-17 3 views
0

안녕하세요, 저는 웹 외부 서버와 통신하기 위해 컬을 사용합니다. 그러나 응답 유형은 html로 json 코드 (4000 행 이상)로 변환 할 수 있었지만 내 결과를 포함하는 지정된 행을 얻는 방법 아이디어. 어떤 생각? 여기 cUrl을 사용하여 지정된 행을 얻는 방법 PHP

내 컬 코드 :

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 (일을 뭔가 달성하기 위해 대신 브라우징 행입니다 : $ 데이터 [ '아이들' [어린이] [어린이] [어린이] [어린이] [어린이] [어린이] [1] [[어린이] 1] [ 'children'] [ 'children'] [ 'children'] [ 'children'] [ 'children'] [0] [ 'children'] [0] [ 'html']은 나에게 좋은 생각), 나는 그것에 직접 가고 싶다.

+0

어떻게 JSON으로 변환 했습니까? 일반적으로 데이터를 구문 분석하고 변수 (배열/객체)에 데이터를 넣은 다음이를 JSON으로 인코딩합니다. 마지막 단계를 건너 뛰고 데이터를 얻었습니다. 보다 자세한 도움이 필요하면 코드와 JSON 구조를 보여주십시오. – GolezTrol

+0

안녕 얘들 아, 내가 몇 가지 정보를 추가, 그것이 나와 함께 공유하는 데 도움이되기를 바랍니다 솔루션 :) 안부. – Zacki

답변

0

반환되는 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]... 
    } 

} 
+0

고맙습니다. @ADyson. – Zacki

관련 문제