0

I get this error when I try to get content from youtube with more then one word.

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

My code:

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

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

However, if I try to get content from youtube using only one word (for example: http://127.0.0.1/sites/mime/beta/default.php?q=html), the script work perfectly.

What's wrong?

pebly
  • 11
  • 6
  • 3
    Possible duplicate of [How to get file\_get\_contents() to work with HTTPS?](http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) – jmattheis Feb 04 '17 at 15:33

1 Answers1

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

This is an url so you must encode before put inside youtube query

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

PHP urlencode

Oscar Zarrus
  • 790
  • 1
  • 9
  • 17