How do I ignore a moved-header with file_get_contents in PHP?

后端 未结 3 558
无人及你
无人及你 2020-12-03 23:59

I have programmed a simple content-user, that uses file_get_contents, but unfortunately for my IP the site now gives a 302 error that forwards to an image. For all other use

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 00:54

    I encountered such a problem accessing Google Drive content via the direct link.

    WRONG WAY: After calling file_get_contents returned 302 Moved temporarily

    //Any google url. Thsi example is fake for Google Drive direct link.
    $url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
    $html = file_get_contents($url);
    echo $html; //print none because error 302.
    

    NICE WAY: With the code below it worked again:

    //Any google url. Thsi example is fake for Google Drive direct link.
    $url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $html = curl_exec($ch);
    curl_close($ch);
    
    echo $html;
    

    I tested it today, 03/19/2018

提交回复
热议问题