通过使用简单的PHP库,我正在使用刮板。我编写了一个函数,对我需要的数据进行擦除。如何从html页面调用该函数?我需要在我的html页面中显示这些数据。我使用的是AngularJS框架,我正在处理视图。
这是我的密码。
function Islamabad_data(){
// Array Declaration
var isb_hi_temp = new Array();
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
echo "Islamabad Data<br>";
echo" <br>High temperature <br>";
foreach($fhtml1->find('#feed-tabs .temp') as $element){
echo $element->plaintext;
?>
isb_hi_temp.push('<?php echo $element; ?>');
<?php
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element)
{
if($i<2)
{
echo $element->plaintext;
?>
isb_hi_temp.push('<?php echo $element; ?>');
<?php
$i++;
}
}
echo isb_data;
}发布于 2015-12-26 09:14:41
我知道这不是一个答案,但我简化了你的代码。看起来,您想要生成一个JavaScript数组,该数组可以通过角来获取。第一步是正确生成所需的JSON。在你的代码中有很多错误--你不能像你所做的那样混合JavaScript和PHP。通常,在PHP中,我们将使用PHP生成一个数组对象,然后调用json_encode来创建将由客户机访问的JSON对象(在本例中,您使用的是角作为客户端)。
function Islamabad_data(){
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
//echo "Islamabad Data<br>";
//echo" <br>High temperature <br>";
$hiTemp = array();
foreach($fhtml1->find('#feed-tabs .temp') as $element){
$hiTemp[] = $element;
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element) {
if ($i<2) {
$hiTemp[] = $element;
}
$i++;
}
echo json_encode( $hiTemp );
}第2步:使用角调用PHP服务。下面是一个过于简化的帮助您的示例:
function Hello($scope, $http) {
$http.get('/some/where/somefile.php').
success(function(data) {
$scope.islamabadData = data;
});
}此外,您应该真正使用Accuweather的API,而不是进行数据抓取。
http://apidev.accuweather.com/developers/
发布于 2016-04-13 09:19:16
它可能对你有帮助:
function loadCitiesData($scope, $http) {
$http.get('/some/where/somefile.php?Islamabad=1').
success(function(data) {
$scope.islamabadData = data;
});
$http.get('/some/where/somefile.php?Lahore=1').
success(function(data) {
$scope.LahoreData = data;
});
}还有你的somefile.php文件:
<?php
if( !empty($_GET['Islamabad']) ){
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
//echo "Islamabad Data<br>";
//echo" <br>High temperature <br>";
$hiTemp = array();
foreach($fhtml1->find('#feed-tabs .temp') as $element){
$hiTemp[] = $element;
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element) {
if ($i<2) {
$hiTemp[] = $element;
}
$i++;
}
echo json_encode( $hiTemp );
}elseif( !empty($_GET['Lahore']) ){
// your code, $yourArray = array('something');
// echo json_encode( $yourArray );
}elseif( !empty($_GET['Multan']) ){
// your code, $yourArray = array('something');
// echo json_encode( $yourArray );
}
?>https://stackoverflow.com/questions/34469511
复制相似问题