doupingpeng7567 2012-10-31 09:26
浏览 65
已采纳

从服务器执行ajax调用和jquery

I have a script that makes an ajax call to yahoo API which then 'crawls' a given URL and sent back the data. Second the data is being send to a service called "Embedly" (which is another call) The embedly service then does its thing and sens back the data which is showed as plain HTML. Please look at the script, its very simple: JsFiddle

$(document).ready(function() {

    baseUrl = "http://www.nu.nl";
    $.ajax({
        url: baseUrl,
        type: "get",
        dataType: "",
        success: function(data) {

            $foop = $('<form>' + data.responseText + '</form>');
            console.log(data.responseText);

            $.each($foop.find('h3 a[href]'), function(idx, item) {
                lnk = $(item).attr("href");
                 text = ('http://www.nu.nl');

$('<fb:like href="' + text + lnk + '"></fb:like><a href="' + text + lnk + '"></a>').appendTo('#content');
});


 $('div#content').embedly({key: 'XXXXXXXXXXXXXXXXXX',
 words: 10
 }); 

        },
        error: function(status) {
            //console.log("request error:"+url);
        }
    });


    });

The problem I'm facing is that sometimes it takes to long to make the calls, especially in the evening and weekends the yahoo API and embedly really take a long time to return data. I would like to solve this problem by making the calls server side with PHP. So that if a visitor visits my site he would receive data from my server instead. Of course the data would have to be 'refreshed' somehow for instance every minute or so. I have googled for this but I cant seem to find anything which describes ajax call with PHP and so on.

  • 写回答

2条回答 默认 最新

  • dpziir0079 2012-10-31 09:43
    关注

    what you have to do is create a server side proxy, create a file and name it YahooAPIProxy.php` inside this file call the yahoo API like

    <?php
    header('Content-Type: text/html; charset=ISO-8859-15');
    function get_url_contents($url){
            $crl = curl_init();
            $timeout = 5;
            curl_setopt ($crl, CURLOPT_URL,$url);
            curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
            $ret = curl_exec($crl);
            curl_close($crl);
            return $ret;
    }
    
        $url = “http://www.nu.nl”;
        $str = file_get_contents($url);
    
        echo $str;
    ?> 
    

    call this proxy from the client side like

    $(document).ready(function() {
    
    
        $.ajax({
            url: '/YahooAPIProxy.php',
            type: "get",           
            dataType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function(data) {
                $foop = $('<form>' + data.responseText + '</form>');
                console.log(data.responseText);
    
                $.each($foop.find('h3 a[href]'), function(idx, item) {
                    lnk = $(item).attr("href");
                     text = ('http://www.nu.nl');
    
    $('<fb:like href="' + text + lnk + '"></fb:like><a href="' + text + lnk + '"></a>').appendTo('#content');
    });
    

    similarly you can repeat the procedure for the Embedly API

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的