2012-03-15 5 views
0

リモートページからイメージを取得する必要がありますが、xpathを試しましたが、imgにnodevalueがないために動作しないと言われました getAttributeを使用するように勧められましたが、それを働かせる方法を知っている。xpathなどの方法でイメージを取得する

提案がありますか? これは私のコードです

<?php 

libxml_use_internal_errors(true); 

//Setting content type to xml! 
header('Content-type: application/xml'); 

//POST Field name is bWV0aG9k 

$url_prefix = $_GET['bWV0aG9k']; 

$url_http_request_encode = strpos($url_prefix, "http://"); 


//Checking to see if url has a http prefix 
if($url_http_request_encode === false){ 
    //does not have, add it! 
    $fetchable_url_link_consistancy_remote_data = "http://".$url_prefix; 

} 
else 
    //has it, do nothing 
{ 
    $fetchable_url_link_consistancy_remote_data = $url_prefix; 
} 



//Creating a new DOM Document on top of pre-existing one 
$page = new DOMDocument(); 

//Loading the requested file 
$page->loadHTMLFile($fetchable_url_link_consistancy_remote_data); 

//Initliazing xpath 
$xpath = new DOMXPath($page); 

//Search parameters 

//Searching for title attribute 
$query = "//title"; 
//Searching for paragraph attribute 
$query1 = "//p"; 
//Searching for thumbnails 
$query2 = "//img"; 


//Binding the attributes to xpath for later use 
$title = $xpath->query($query); 

$paragraph = $xpath->query($query1); 

$images = $xpath->query($query2); 




echo "<remotedata>"; 
//Echoing the attributes 
echo "<title-render>".$title->item(0)->nodeValue."</title-render>"; 
echo "<paragraph>".$paragraph->item(0)->nodeValue."</paragraph>"; 
echo "<image_link>".$images->item(0)->nodeValue."</image_link>"; 
echo "</remotedata>"; 

?> 
+0

ヒントのimg/@のSRCの値を必要とし、値を持ちません。 '//コンテンツタイプをxmlに設定する!'は、次の行に 'header( 'Content-type:application/xml');他のすべてのコメントと同じこと。 – Tomalak

+0

それとは別に、あなたが扱っているXMLを見ることなく、誰もあなたを助けることができません。 – Tomalak

+0

checkin phpqueryを試してください...助けてください。 – hummingBird

答えて

1

イメージタグのソース属性を取得する必要があります。次の行が何を伝えるためにコメントを使用しないでください。これは、通常のXHTMLであれば

$images->item(0)->getAttribute('src'); 
+0

最初にgetElementByTagNameを実行して、imgタグだけのsrcを取得するようにしてください。 –

+0

私はdomにhtmlを読み込んだと仮定します。 $ query2 = "// img"; //すべての画像タグを選択します $ images = $ xpath-> query($ query2); // $ imagsはすべてのイメージタグを選択しています $ images-> item(0) - > getAttribute( 'src'); //最初の画像のsrc属性値を返す – CoreCoder

+0

はい、これをdomにロードしました –

1

は、IMGあなたが