【问题标题】:How to call a Php Function from html如何从 html 调用 PHP 函数
【发布时间】:2016-03-31 21:15:14
【问题描述】:

我通过使用简单的 PHP DOM 库来使用 PHP scraper。我写了一个函数来抓取我需要的数据。如何从 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;
}

【问题讨论】:

  • 你能告诉我们Angular模块和服务代码吗?

标签: php jquery ajax html


【解决方案1】:

我知道这不是答案,但我已经简化了您的代码。看起来你想生成一个 Angular 可以获取的 JavaScript 数组。第 1 步是正确生成所需的 JSON。您的代码中有很多错误——您不能以您正在做的方式混合 JavaScript 和 PHP。通常在 PHP 中我们会使用 PHP 生成一个数组对象,然后调用 json_encode 来创建将被客户端访问的 JSON 对象(在这种情况下,您使用 Angular 作为您的客户端)。

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 步。使用 Angular 调用您的 PHP 服务。这是一个过于简单的示例来帮助您:

function Hello($scope, $http) {
    $http.get('/some/where/somefile.php').
        success(function(data) {
            $scope.islamabadData = data;
        });
}

另外,您应该真正使用 Accuweather 的 API,而不是进行数据抓取。

http://apidev.accuweather.com/developers/

【讨论】:

  • 谢谢克莱顿。但是在我的 .php 文件中,我有多个函数,每个函数都返回 3 个数组...是否可以从 angular 调用该函数?
  • 是的,看看我的例子。在 Angular 中,您使用 $http.get()。您需要修改代码,使其与我为 Angular 发布的代码相似,以使所有内容都采用正确的格式。
  • 感谢 Clyaton,我在使用该功能时遇到了问题。 $http.get('/some/where/somefile.php')。成功(函数(数据){ $scope.islamabadData = 数据;});
【解决方案2】:

也许对你有帮助:

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 );
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多