Google News API - Using Curl with PHP

Asked

Viewed 129 times

0

I’m trying to use the API of Google News, can’t list the news I want from a certain category, I would like to do with PHP so that you can list and store them in the database.

I took an example from Internet:

$details_url = "https://newsapi.org/v2/everything?q=video-games&apiKey=minhachave";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo $geoloc = json_decode(curl_exec($ch), true);

You’re not listing anything, someone has experience with this api?

1 answer

0


I round up the test and the way your code is it gives an error message like this:

SSL Certificate problem: Unable to get local Issuer Certificate

then to get around that which is the verification of the SSL configure your code to not do this check:

$details_url = 'https://newsapi.org/v2/everything?q=video-games&apiKey=minhachave';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // aqui a alteração
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // aqui a alteração
$result = json_decode(curl_exec($ch), true);
echo '<pre>';
print_r($result);

from these two settings the result is returned. A recommended reading on: PHP CURL issues "Unable to get local Issuer Certificate" error here on the site itself.

Observing: I recommend this only in a test environment, in production usually the servers have the certificate and can be used without these settings and remember that security is a topic debated and should exist to help maintain a healthy system.

Summary: your code requires verification SSL where your server is not configured correctly and the result is not brought, showing the error reported in this response and an example code was made that can be used in your test environment, but, not recommended in the production environment, for the reason security guard.

Browser other questions tagged

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