Php error youtube

0

I was using the code below to list the video name through the YouTube API 2 weeks ago.

Code:

<?php

$dados ="Roberto Carlos";

$video_list = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($dados).'&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc'));?>



<?php foreach($video_list->items as $item){?>


Titulo da musica : <?php echo $item->snippet->title;?>


<?php } ;?>

You are now giving the following error:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed 

How can I resolve this error?

    
asked by anonymous 15.03.2018 / 02:15

2 answers

0

This happens because you are not setting up a valid certificate in the openssl a> in your PHP.

There are several alternatives to correct or "circumvent" this problem

Downloading and configuring a valid certificate

If you do not have a valid SSL certificate to configure, you can download the link link after downloading this certificate , just add the code below into your php.ini and then restart your server.

openssl.cafile=/path/to/cacert.pem
  

Use tools like link , check the validity of a certificate.

Disabling SSL on file_get_contents

If for some reason you do not want to use SSL, you can create an object < in> stream_context . In this object you can inform that you do not want to make a secure request.

In the function file_get_contents we can pass three parameters:

  • $filename : File name or URL;
  • $flags : The way we want to access content ( FILE_BINARY , FILE_TEXT etc);
  • $context : Object created from function stream_context_create .

Example:

<?php

$dados = "Roberto Carlos";
$url   = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($dados).'&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc';

$stream = stream_context_create([
    'ssl' => [                 /* Configuração do openssl da requisição */
        'verify_peer' => false /* Ignora a necessidade do certificado e desabilita requisição segura */
    ]
]);

$result = file_get_contents($url, FILE_BINARY, $stream);

$video_list = json_decode($result);

foreach($video_list->items as $item){;
    var_dump($item->snippet->title);
}

Informing a certificate with file_get_contents

If you have limitations on your server and can not modify the php.ini file, you can use stream_context to enter the path of the certificate you generated or downloaded, for example:

<?php

$dados = "Roberto Carlos";
$url   = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($dados).'&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc';

$stream = stream_context_create([
    'ssl' => [                               /* Configuração do openssl da requisição */
        'verify_peer' => true,               /* Habilita requisição segura */
        'cafile'      => '/pasta/cacert.pem' /* Informe a pasta onde você baixou/gerou seu certificado */
    ]
]);

$result = file_get_contents($url, FILE_BINARY, $stream);

$video_list = json_decode($result);

foreach($video_list->items as $item){;
    var_dump($item->snippet->title);
}

Using cURL

Another possibility is to use the cURL to make requests . This option is more robust, but will similarly make a request.

To configure how our request is to be made, we need to use the curl_setopt function. It is in this function that we will inform if the request must be of type POST or GET ; Whether we can "follow" the redirects or not; Whether or not to ignore the use of the openssl certificate and etc.

Commented example:

<?php

$dados = "Roberto Carlos";
$url   = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($dados).'&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc';

/* Instancia o cURL e definimos a URL que desejamos passar */
$ch = curl_init($url);

/* Informamos que queremos receber o retorno da requisição */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/**
 * Desabilita a necessidade do certificado, ignorando a segurança
 * Atenção: Remova a linha abaixo, caso você já tenha configurado o 'php.ini'
 */
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

/* Realizamos a requisição e obtermos o resultado */
$result = curl_exec($ch);

/* Fechamos a requisição */
curl_close($ch);

$video_list = json_decode($result);

foreach($video_list->items as $item){;
    var_dump($item->snippet->title);
}

Using cURL with SSL

If you have limitations on your server and can not modify the php.ini file, you can tell the location of the generated / downloaded certificate in the cURL configuration:

Commented example:

<?php

$dados = "Roberto Carlos";
$url   = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($dados).'&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc';

/* Instancia o cURL e definimos a URL que desejamos passar */
$ch = curl_init($url);

/* Informamos que queremos receber o retorno da requisição */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/* Indique o local do arquivo *.pem que você baixou/gerou */
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/cacert.pem");

/* Realizamos a requisição e obtermos o resultado */
$result = curl_exec($ch);

/* Fechamos a requisição */
curl_close($ch);

$video_list = json_decode($result);

foreach($video_list->items as $item){;
    var_dump($item->snippet->title);
}
    
15.03.2018 / 03:59
0

<?php

$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

$video_list = file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=roberto&maxResults=3&key=AIzaSyAVittweX_WS_VQYypeYl5uDSWl2ti7PMc", false, stream_context_create($arrContextOptions));

echo $video_list;
?>

Now I need to list each title I'm doing so even more is giving error

<?php foreach($video_list->items as $item){;?>
	
<?php echo $item->snippet->title;?><br>

<?php } ;?>
    
15.03.2018 / 03:41