H
H
Herman Martin2018-03-31 13:13:59
YouTube
Herman Martin, 2018-03-31 13:13:59

How to overcome the error with file_get_contents failed to open stream: HTTP request failed! HTTP/1.0 400 - https youtube?

Good afternoon.
There is a code:

$url = 'https://www.googleapis.com/youtube/v3/videos';
$cn_match = 'www.googleapis.com';
// $url = 'https://stackoverflow.com/questions/';
// $cn_match = 'stackoverflow.com';

$data = array (     
  'key' => $api_key,               
  'part' => 'snippet',
  'id' => $video_id
);

// use key 'http' even if you send the request to https://...
$options = array(
  'http' => array( // text/plain
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'method'  => 'GET', 
    'content' => http_build_query($data)                
  )
  , 'ssl' => array(
    'verify_peer' => true,
    'cafile' => '/SRV/php721/extras/ssl/' . "cacert.pem",
    'ciphers' => 'HIGH:TLSv1.2:TLSv1.1:TLSv1.0:!SSLv3:!SSLv2',
    'CN_match' => $cn_match,
    'disable_compression' => true,
  )
);

$scu = $url . '?' . http_build_query($data);
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);
//$response = file_get_contents($scu);
//echo Debug::d($context);
//echo $scu; 
echo Debug::d($response);

execution results in an error:
Warning: file_get_contents(https://www.googleapis.com/youtube/v3/videos): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\SRV\vhosts\test.loc\html\tmp\youtube-api\ex1\t1.php on line 67

or simply HTTP/1.0 400 Bad Request
if you do a curl, for example, like this:
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$video_id&key=$api_key");
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

$jsonData = json_decode(curl_exec($curlSession));
curl_close($curlSession);

echo Debug::d($jsonData);

there are no errors, however, how to make the original code work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Herman Martin, 2018-03-31
@dklight

the issue is resolved, however, it did not work through file_get_contents, I had to use fopen:

$scu = $url . '?' . http_build_query($data);
$opts = array('http' =>
    array(
        'method' => 'GET',
        'max_redirects' => '0',
        'ignore_errors' => '1',        
    )
    , 'ssl' => array(
    'verify_peer' => true,
    'cafile' => '/SRV/php721/extras/ssl/' . "cacert.pem",
    'ciphers' => 'HIGH:TLSv1.2:TLSv1.1:TLSv1.0:!SSLv3:!SSLv2',
    'CN_match' => $cn_match,
    'disable_compression' => true,
  )
);

$context = stream_context_create($opts);
$stream = fopen($scu, 'r', false, $context);

// информация о заголовках, а также
// метаданные о потоке
echo Debug::d(stream_get_meta_data($stream),'stream_get_meta_data($stream)');

// актуальная информация по ссылке $url
echo Debug::d(stream_get_contents($stream),'stream_get_contents($stream)');
fclose($stream);

L
Lazy @BojackHorseman PHP, 2018-03-31
Tag

I think that in the GET request you need to pass parameters through the url. Seems like it's always been like this, right? in the second case, this is what happens. and in the first case they are not in $ url
and in general 400 is when the structure of the http request is broken, but you can also return a header with such an error if the expected parameters are not received

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question