2017-02-04 59 views
0

我嘗試從多個單詞獲取YouTube內容時遇到此錯誤。HTTP請求失敗!嘗試從YouTube獲取內容時出現HTTP/1.0 400錯誤請求

Warning: file_get_contents(https://www.youtube.com/results?search_query=html begginer): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\sites\mime\beta\default.php on line 5 

我的代碼:

<?php 
    $address = $_GET['q']; 
    $address = 'https://www.youtube.com/results?search_query=' . $address; 

    $html = file_get_contents($address); 
    echo $html; 
?> 

不過,如果我嘗試使用只有一個字(例如:http://127.0.0.1/sites/mime/beta/default.php?q=html),以獲得來自YouTube的內容,腳本工作完美。

怎麼了?

+3

的【如何獲取文件\ _get \ _contents()與HTTPS工作可能的重複? ](http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) – jmattheis

回答

0
$address = $_GET['q']; // this GET can be spaced or not 

這是一個網址,所以你必須編碼把以前的YouTube查詢中

$address = urlencode($_GET['q']); 

PHP urlencode

相關問題