Php error api youtube

Asked

Viewed 90 times

0

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

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

Now you’re making the following mistake:

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 fix this mistake?

  • That key is valid ?

  • Yes and validates already tested

  • https://www.googleapis.com/youtube/v3/search?part=snippet&q=Roberto%20Carlos&maxResults=3&key=Aizasyavittwex_ws_vqyypeyl5udswl2ti7pmc

  • a look up here url

  • 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

  • I had already seen my director but I did not understand and I could not solve

  • 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.

  • Okay my director I’ll wait

  • Hi my director is still around ?

  • Got it that way I’ll put in a reply the code

Show 6 more comments

2 answers

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 more is giving error

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

<?php } ;?>

0


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);
}
  • Dude you did it all Thank you so much Now I get it !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.