JSON Problem

Discussion in 'PHP' started by ssimon171078, Feb 20, 2015.

  1. #1
    I want to scrappe data from fiverr.com i write php code :
    But its not returing data. I also tried with curl as well. No success at all.
    <?php
    $url ='https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
    $html = file_get_contents( $url);
    echo $html;
    ?>
     
    ssimon171078, Feb 20, 2015 IP
  2. Dominic Ceraso

    Dominic Ceraso Member

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #2
    I believe the problem may be - fivver.com sets some cookies and then tells you to send another request to the same URL with your new set of cookies. PHP's
    file_get_contents()
    Code (markup):
    will not send cookies by default and will put you into a 302 redirect loop.

    i would manually manage the cookies something like;

    $url  = 'http://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
    $opts = array('http' => array(
        'header' => 'Cookie: locale=en%3B0%3Bfalse; suggested_locale=1;',
    ));
    $ctx  = stream_context_create($opts);
    $data = file_get_contents($url, false, $ctx);
    Code (markup):
    the variable $data contains binary data since the site gzipped the contnet. so you may want to have plain data.

    $data = gzedecode($data);
    Code (markup):
    Now you will have JSON encoded data that you can parse using
    json_decode()
    Code (markup):
    hope this helps.
     
    Dominic Ceraso, Feb 20, 2015 IP
  3. Vladimir Lisovets

    Vladimir Lisovets Active Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    jQuery

    $.ajax({
    url:'proxy.php',
    type:'POST',
    data:{
    address:'http://www.google.com'},
    success:function(response){

    alert(response);

    }});

    PHP (proxy.php)

    echo file_get_contents($_POST['address']);
     
    Vladimir Lisovets, Feb 23, 2015 IP