0

我已经编写了从dailyhoroscopes.com 网站获取星座的代码:

<?php
include('../simple_html_dom.php');

// get DOM from URL or file
$html = file_get_html('http://www.dailyhoroscope.com/?date=yesterday');

$needle = 'var ar_interps = ';
$needle_end = 'var ar_interps_love = ';

foreach($html->find('script') as $e)       
    if (strpos($e->innertext, $needle) !== false) {
        $json = substr($e->innertext, strpos($e->innertext, $needle));        
        $json = substr($json, 0, strpos($json, $needle_end));
        $json = str_replace($needle, '', $json);
        $json = str_replace(';  ', '', $json);
        echo '<pre>';
        var_dump(json_decode($json, true));
        echo '</pre>';
        }
?>

仅使用 html 和 javascript 进行相同操作的最佳方法是什么?

4

2 回答 2

0

没有办法:这个服务器不支持 CORS。对于跨域资源共享,请参阅维基百科文章。http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

于 2013-04-09T19:00:40.673 回答
0

你不能。跨域访问策略不允许网页访问从其他来源加载的内容。dailyhoroscope.com在这种情况下,除非您的页面也在该域上,否则这将不允许您从中加载数据。

此外,未经他们的许可重复使用他们的内容很可能构成侵犯版权,即使这不是非法的,也是一种鸡巴举动,因为它最终会在他们的服务器上产生负载。不要这样做。

于 2013-04-09T19:01:46.413 回答