2014-09-02 7 views
0

온라인 상점에서 가격을 가져 오려고합니다. 이 코드를 사용하고 여기에가져올 수 없습니다.

..

<?php 
function getPrice($site){ 
    $html = file_get_contents($site); 
    $dom = new DOMDocument(); 
    $dom->loadHTML($html); 
    $contents = $dom->document.getElementsByTagName("span");   

    $price = ""; 
    for($i = 0; $i < $contents->length; $i++){ 
     $content= $contents->item($i); 
     if($content->getAttribute('class') == "fk-font-verybig pprice vmiddle fk-bold"){ 
      $price = $content->getAttribute('value'); 
     } 
    } 
    echo $price; 
} 

$website = "http://www.flipkart.com/sogo-ss-5365-750-w-pop-up-toaster/p/itmdz3hgfjzgfp4v?pid=PUTDYWT2UHPCDCG8&offer=DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2."; 

getPrice($website); 


?> 

내 스크립트 반환 오류

경고 : DOMDocument를 :: loadHTML() : 예기치 않은 종료 태그 : E 261 : 엔티티, 줄 간격 : \ 5 호선

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 5 

................................................................... 

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 6160 in E:\Local server\htdocs\store\scripts\getprice.php on line 5 

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6 

Fatal error: Call to undefined function getElementsByTagName() in E:\Local server\htdocs\store\scripts\getprice.php on line 6 

로컬 서버 \ htdocs에의 \ 저장 \ 스크립트 \ getprice.php 가게가 제품의 가격을 계속 변경 있기 때문에 확인이 같은 가격을 가져올 것입니다. 그렇게 할 수있는 다른 방법이 있습니까?

이 스크립트는 내 웹 사이트에서 제품 페이지를 방문 할 때마다 5 가지 상점에서 가격을 공백으로 가져 오기 때문에 내 서버 성능에 영향을 미칩니 까?

+1

웹 사이트에서 가격을 가져올 수있는 공식적인 법적 권한이있는 경우 xml 또는 csv 형식의 가격 정보를 얻기 위해 해당 업체에 연락해야합니다. 네, 페이지로드에 영향을줍니다. 가격을 캐시해야합니다 (memcache, redis, sql). – DanFromGermany

+0

그냥 도트를 제거하고 DOTDOnPopUpToaster_Sep2 한번 시도해보십시오. & icmpid = hp_dotd_3_DOTDOnPopUpToaster_Sep2.? –

+0

양식 서버를 다시 가져 오는 시간을 줄이기 위해 MySQL에 가격을 저장하려고 생각합니다. – user3811305

답변

0
$contents = $dom->document.getElementsByTagName("span"); 

DOMDocument 클래스에 '문서'라는 속성이 없으므로 $ dom-> 문서가 실패합니다.

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6 

그래서이

$contents = $dom->getElementsByTagName("span"); 

는 위의 작업을해야 작동 할 수 있습니다.

에코 대신 $ 내용을 반복하는 것이 좋습니다.

print_r조차도 $ contents의 노드 구조를 보는 데 도움이됩니다.

+0

Thnx는 도움이되었지만 지금은 경고 만 받고 출력은 없습니다 – user3811305

+0

경고는 무엇입니까? – MontrealDevOne

+0

경고 : DOMDocument :: loadHTML() : 예기치 않은 종료 태그 : 행의 엔터티, 줄 : 261 in E : \ 로컬 서버 \ htdocs \ store \ scripts \ getprice.php 6 행 경고 : DOMDocument :: loadHTML() : htmlParseEntityRef : Entity에서 이름 없음, 줄 : 293 in E : \ Local server \ htdocs \ store \ scripts \ getprice.php on line 6 경고 : DOMDocument :: loadHTML() : htmlParseEntityRef : 엔터티에 이름이 없습니다. 줄 : 293 in E : \ Local server \ htdocs \ store \ scripts \ getprice.php 6 행 .... 경고 : DOMDocument :: loadHTML() : htmlParseEntityRef : 예상 ';' 엔터티에서 줄 : 6125 E : \ Local server \ htdocs \ store \ scripts \ getprice.php 6 줄에 – user3811305