Geting remote file using CURL

Discussion in 'PHP' started by messer00, Jun 23, 2010.

  1. #1
    Okay... Im new to using CURL so i hope someone will help me.

    Basically I need to download remote file to the directory on my server.

    My script is doing a search across titles in database, then take from it their id and pass it to $aid variable.

    Rest is in snippet:

    
    function get_contents($url) {
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch,CURLOPT_PORT,9001);
    
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
        
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        curl_setopt($ch, CURLOPT_ENCODING, "utf-8");
    
        $string = curl_exec($ch) or die(curl_error());
    
        curl_close($ch);
    
        return $string;
    
    }
     
    
    ...blablabla
    
    $adres = 'http://api.anidb.net9001/httpapi?request=anime&client=animerank&clientver=1&protover=1&aid='.$aid;
    $con = get_contents($adres);
    $had = './aid_xml/'.$aid.'.xml';
    $ha = fopen($had,"w");
    	fputs($ha,$con);
    	fclose($ha);
    
    ..rest
    PHP:
    Unfortunately im not geting normal file like i would typing for example http://api.anidb.net:9001/httpapi?request=anime&client=animerank&clientver=1&protover=1&133

    in my browser. Instead contents are standard 404 page. I believe that is the page received if not connected on 9001 port. But I think I done this part with curl
    curl_setopt($ch,CURLOPT_PORT,9001);
    PHP:

    What am I doing wrong?

    Definition of the api: http://wiki.anidb.info/w/HTTP_API_Definition
     
    messer00, Jun 23, 2010 IP
  2. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #2
    georgiivanov, Jun 23, 2010 IP
  3. messer00

    messer00 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats not the case. I made a mistake posting a link but script still doesnt work.

    Content is gzipped and utf-8 encoded, but i think Curl decompress gzpipped pages automatically.
     
    Last edited: Jun 23, 2010
    messer00, Jun 23, 2010 IP
  4. SwedBo

    SwedBo Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have you tried to remove the port from the given URL ?
     
    SwedBo, Jun 23, 2010 IP
  5. messer00

    messer00 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes - not working.

    Maybe there is a better way to download this content?
     
    Last edited: Jun 23, 2010
    messer00, Jun 23, 2010 IP
  6. georgiivanov

    georgiivanov Member

    Messages:
    62
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    25
    #6
    set the encoding with curl_setopt:
     
    georgiivanov, Jun 23, 2010 IP
  7. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Therefore change the CURL_ENCODING TO Gzip and have a try!
     
    roopajyothi, Jun 23, 2010 IP
  8. messer00

    messer00 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think that did it. Thank you for your help.
     
    messer00, Jun 24, 2010 IP