This happens because you are not setting up a valid certificate in the settings of openssl in your PHP.
There are several alternatives to fix 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 https://curl.haxx.se/ca/cacert.pem, after downloading this certificate, just add the code below in your php.ini
and then restart your server.
openssl.cafile=/path/to/cacert.pem
Use tools such as https://www.sslshopper.com/certificate-decoder.html, check the validity of a certificate.
Disabling the SSL on file_get_contents
If, for some reason, you don’t want to use SSL, you can create an object stream_context. In this object you can inform that you do not want to perform a secure request.
In 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 the 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 cannot modify the file php.ini
, you can use the stream_context to inform 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 should be made, we need to use the function curl_setopt
. It is in this function that we will inform if the request should be of the type POST or GET; Whether we can "follow" the redirects or not; Whether or not we should 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 cannot modify the file php.ini
, you can enter the generated/downloaded certificate location 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);
}
That key is valid ?
– AnthraxisBR
Yes and validates already tested
– Hemerson Prestes
https://www.googleapis.com/youtube/v3/search?part=snippet&q=Roberto%20Carlos&maxResults=3&key=Aizasyavittwex_ws_vqyypeyl5udswl2ti7pmc
– Hemerson Prestes
a look up here url
– Hemerson Prestes
See the answer to this question : https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more. will probably work if it doesn’t serve comment there again
– AnthraxisBR
I had already seen my director but I did not understand and I could not solve
– Hemerson Prestes
That answer over there shows how to disable peer checking, will allow you to connect without security, I have no way to test a code now, if no one responds tomorrow I make a response.
– AnthraxisBR
Okay my director I’ll wait
– Hemerson Prestes
Hi my director is still around ?
– Hemerson Prestes
Got it that way I’ll put in a reply the code
– Hemerson Prestes
Let’s go continue this discussion in chat.
– Hemerson Prestes