Thursday, January 9, 2014

PHP Code for RSS Feed using fopen behind Proxy

Pada case ini digunakan script otentikasi sbb:

==================================================
$auth = base64_encode('username:password'); // user password proxy anda

$aContext = array(
    'http' => array(
        'proxy' => 'yourproxy:yourproxyport', // Proxy anda disini
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);

$default = stream_context_get_default($aContext);

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

berhubung sudah dijadikan default maka koneksi untuk RSS akan melalui proxy tersebut.
Berikut semua kode php untuk menampilkan RSS Feed melalui proxy.

<?php

$auth = base64_encode('username:password'); // user password proxy anda

$aContext = array(
    'http' => array(
        'proxy' => 'yourproxy:yourproxyport', // Proxy anda disini
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);

$default = stream_context_get_default($aContext);

    $rss = new DOMDocument();
    $rss->load('http://tempo.co/rss/teknologi');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
//            'media' => $node->getElementsByTagName('media:content url')->item(0)->nodeValue,

            );
        array_push($feed, $item);
    }
    $limit = 10;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
//        $image = $feed[$x]['media'];

        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        echo '<small><em>Posted on '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
//        echo '<img src="' . $image . '"/>';

    }
?>

No comments:

Post a Comment