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);
}