Membaca Isi URL Lain dengan PHP

Discussion in 'Programming' started by anton_sudibyo, Dec 25, 2015.

  1. anton_sudibyo

    anton_sudibyo Member

    Joined:
    Dec 23, 2015
    Messages:
    459
    Likes Received:
    35
    Trophy Points:
    28
    Untuk membaca isi (konten) URL lain alias scraping dengan PHP ada beberapa cara.

    1. Cara paling simpel dengan file_get_contents()
    Hanya bisa bekerja kalau "allow_url_fopen" diperbolehkan di setting PHP.
    PHP:
    <?php
    header
    ('Content-type: text/plain');

    echo 
    file_get_contents('http://www.bersosial.com');
    ?>
    2. Pakai fopen
    PHP:
    <?php
    header
    ('Content-type: text/plain');

    $handle fopen('http://www.bersosial.com''r');
    $contents stream_get_contents($handle);
    fclose($handle);

    echo 
    $contents;
    ?>
    3. Pakai curl.
    Hanya bisa bekerja kalau modul php curl terinstall di server.
    PHP:
    <?php
    header
    ('Content-type: text/plain');

    // create a new cURL resource
    $ch curl_init();

    // set URL and other appropriate options
    curl_setopt($chCURLOPT_URL"http://www.kaskus.co.id/");
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
    @
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);

    // grab URL and pass it to the browser
    $content curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    echo 
    $content;
    ?>
    Selamat mencoba :D
     
  2. Samsul Arifin

    Samsul Arifin Member

    Joined:
    Feb 9, 2015
    Messages:
    54
    Likes Received:
    1
    Trophy Points:
    8
    Saya paling suka yang nomor 3 gan. Biasanya dipakek untuk baca API suatu website kan ya?
    Misal ngambil API dari tiket.com untuk pencarian tiket. Cuma tadi saya coba coding agan yang nmor 3 kok blank ya?
     
  3. anton_sudibyo

    anton_sudibyo Member

    Joined:
    Dec 23, 2015
    Messages:
    459
    Likes Received:
    35
    Trophy Points:
    28
    Ada dokumentasi API nya den? :)
     
  4. Samsul Arifin

    Samsul Arifin Member

    Joined:
    Feb 9, 2015
    Messages:
    54
    Likes Received:
    1
    Trophy Points:
    8
  5. anton_sudibyo

    anton_sudibyo Member

    Joined:
    Dec 23, 2015
    Messages:
    459
    Likes Received:
    35
    Trophy Points:
    28
    Oh ternyata url API nya https ya. Kalau https ada kode tambahan. Bisa cek kode di bawah :)

    PHP:
    <?php
    header
    ('Content-type: text/plain');

    // create a new cURL resource
    $ch curl_init();

    // set URL and other appropriate options
    curl_setopt($chCURLOPT_URL"https://api-sandbox.tiket.com/order?token=c992866a6ffb08e59a86fc6a050ca7c7bdec6c2f");
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_AUTOREFERERtrue);
    @
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);

    // SSL
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);

    // grab URL and pass it to the browser
    $content curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    echo 
    $content;
    ?>
     
  6. idemika

    idemika New Member

    Joined:
    Jan 7, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
Loading...

Share This Page