Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

    

public function indexAction() {
    $result = $this -> crawl_page("website crawler");
    $this -> view -> result = $result;
}

function crawl_page($url){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec ($ch);

    curl_close ($ch);
    return $result;

}

Sau đó sử dụng javascript lấy dữ liệu đã nhận được, lọc ra để sử dụng.

Link tham khảo : http://hayageek.com/php-curl-post-get/
$('#read-more').on('click', function() {
        var dataajax = {'read-more-from': $("#read-more-from").val()};
        // gọi ajax
        $.ajax({
            url : "/news/readmore",
            type : 'POST',
            data : dataajax,
            dataType : 'json',
            success: function(data){
                // gọi function append vào html
                addNewsAjax(data.arrNews);
                $("#read-more-from").val(data.offset);
            }
        });
    });

=================================

public function readmoreAction() {
        if ($this -> request -> isPost()) {
            $this -> view -> disable();
            // lấy dữ liệu truyền thông qua ajax
            $offset = $this->request -> getPost('read-more-from');

            $news = new News();
            // lấy dữ liệu từ DB
            $newsList = $news -> findByLimit(10, $offset);
            // khai báo array chứa dữ liệu để loop ngoài html
            $arrNews = array();
            foreach ($newsList as $news) {
                $arr = array('id'             => $news -> id,
                                'title'              => $news -> title,
                                'description'   => $news -> description,
                                'content'         => $news -> content,
                                'url'                => $news -> url,
                                'image'           => $news -> image,
                                'updated_at'   => date('d-m-Y', strtotime($news -> updated_at)));
                array_push($arrNews, $arr);
            }
            // output json ra ngoài javascript
            echo json_encode(array('arrNews' => $arrNews, 'offset' => ($offset + 10)));
        }
    }
==================================
<a href="javascript:void(0)" id="read-more">Xem tiếp >></a>