2013-05-08 44 views
1

我想从下面的代码的IMG SRC标记特定的标签:获取从API代码

<pod title="Scientific name" scanner="Data" id="ScientificName:SpeciesData" position="200" error="false" numsubpods="1"> 
<subpod title=""> 
<plaintext>Canis lupus familiaris</plaintext> 
<img src="http://www4a.wolframalpha.com/Calculate/MSP/MSP17941cd1c5fi21h72ac2000057i1ae7gc4986gdf?MSPStoreType=image/gif&s=44" alt="Canis lupus familiaris" title="Canis lupus familiaris" width="139" height="18"/> 
</subpod> 
</pod> 

我知道怎么弄的明文信息,但我怎么得到IMG SRC信息?这是我必须得到明文信息:

<?php 
if(isset($_POST['q'])){ 
include 'WolframAlphaEngine.php'; 
$engine = new WolframAlphaEngine('APP-ID'); 

$resp = $engine->getResults("$q"); 

$pod = $resp->getPods(); 

$pod1 = $pod[1]; 


foreach($pod1->getSubpods() as $subpod){ 
    if($subpod->plaintext){ 
    $plaintext = $subpod->plaintext; 
    break; 
    } 
} 

$result = substr($plaintext, 0,strlen($plaintext)-3); 

echo "$plaintext"; 

} 
?> 

这不是Grabbing the href attribute of an A element重复,因为我不能在我的Godaddy主机使用DOM。我已经尝试过。

回答

0

我从未与WolframAlphaEngine合作过。我想这是这一个:

如果是这样,看看https://github.com/brkeerthi/chethana/blob/master/include/WASubpod.php

class WASubpod { 
    // define the sections of a response 
    public $attributes = array(); 
    public $plaintext = ''; 
    public $image = ''; 
    public $minput = ''; 
    public $moutput = ''; 
    public $mathml = ''; 

正如你所看到的,它不仅为明文,而且对性能属性和其他东西。这可能就像

foreach ($pod1->getSubpods() as $subpod) { 
    $attributes = $subpod->attributes; 
    if (isset($attributes['src']) { 
     echo $attributes['src']; 
    } 
} 

但是由于该库的文档是不存在的,所以我不知道。所以,如果这不起作用,只需var_dump$subpod看看它包含什么。

同样,看看其他类,找出他们做了什么:

+0

非常接近!我做了你的代码,它只是一个数组[标题]的东西...所以我做了foreach($ pod1-> getSubpods()as $ subpod){print_r($ subpod-> image); }'代替atrributes和它给我' WAImage对象 ( [属性] =>数组 ( [SRC] => http://www4b.wolframalpha.com/Calculate/ MSP/MSP7141hbbg7330dcd6h4f00002cb39d647363ei1g?MSPStoreType =图像/ GIF&S = 55 [ALT] =>家犬 [标题] =>家犬 [宽度] => 139 [高度] => 18 ) )'现在我如何从t获取src帽子? – user2362601 2013-05-08 14:24:45

+0

@ user2362601根据转储:'$ subpod-> image-> attributes ['src']'应该可以工作 – Gordon 2013-05-08 14:33:57

+0

谢谢Gordon !! – user2362601 2013-05-08 14:37:23