2017-05-29 28 views
0

我试图在我的网站上显示€(数字)https://www.stratisrate.com/,但我不知道如何单独显示它并将其变为变量而不是显示整个事情。我有这样的代码,现在PHP file_get_contents - 不知道如何选择特定类/ div

<?php 

     $content = file_get_contents("https://www.stratisrate.com/"); 

     echo $content; 

    ?> 

但我不知道怎么只显示号码(我已经看过几个教程,但我仍然无法搞清楚。

+2

阅读有关的DomDocument和解析HTML – splash58

+0

这篇文章可能帮助你.. HTTP:// www.the-art-of-web.com/php/html-xpath-query/ –

+0

@ splash58已经尝试过,但仍然无法弄清楚 – FluffyMe

回答

4

你可以分析DOM的建议。在评论中,也可以利用用来刷新通过AJAX内容JSON响应的

试试这个:

$result = json_decode(file_get_contents('https://www.stratisrate.com/json/load/rate.json'), true); 

// this will print the whole array under the key 'euro' 
print_r($result['euro']); 

//This will get you just the value you asked for 
print_r($result['euro']['rate']); 

JSON响应看起来是这样的:

{ 
    "euro": { 
     "rate_title": "euro", 
     "rate": "3.37428419", 
     "strat": 0.296359, 
     "symbol": "&euro;" 
    }, 
    "dollar": { 
     "rate_title": "dollar", 
     "rate": "3.33987", 
     "strat": 0.299413, 
     "symbol": "&#36;" 
    }, 
    "bitcoin": { 
     "rate_title": "bitcoin", 
     "rate": "0.00163", 
     "strat": 613.496933, 
     "symbol": "Ƀ" 
    } 
} 

我通过查看谷歌浏览器开发工具中网络选项卡下的XHR请求发现了这一点。 Ajax request to update content

然后,我只是检查打开该XHR调用的URL,并得到了结果(无身份验证令牌或登入)

+0

这是一个不同的东西比我试过,但它的工作非常感谢 – FluffyMe

+0

很好的解决方案+1,为您的搜索:) –

+0

@FluffyMe - 你应该这样做,而不是抓取HTML肯定,如果它有你需要的数据。 –