警告file_get_contents()无法打开流:HTTP请求失败! HTTP / 1.0 402需要付款

时间:2015-03-11 19:20:48

标签: php curl youtube

我今天收到此错误,但在此之前,我的代码工作正常。

代码在localhost上工作正常,但我的网站上出现错误

以下是产生错误的代码行:

$data = file_get_contents("https://www.youtube.com/get_video_info?hl=en&video_id={$_GET['id']}&eurl=&el=detailpage&gl=US&ps=default");

和错误:

  

警告:file_get_contents(https://www.youtube.com/get_video_info?hl=en&video_id=SD3S27fMGzc&eurl=&el=detailpage&gl=US&ps=default):无法打开流:HTTP请求失败! HTTP / 1.0 402需要付款   在 /home/zyxs/public_html/site.com/vid/youtube.php 2

1 个答案:

答案 0 :(得分:0)

试试这个

function http_get_contents($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if(FALSE === ($retval = curl_exec($ch))) {
            echo curl_error($ch);
        } else {
            return $retval;
        }
    }

    $request_url = 'http://gdata.youtube.com/feeds/api/videos/SD3S27fMGzc';
    $result = http_get_contents($request_url);

    $simpleXML = new SimpleXMLElement($result);

    print_r($simpleXML);