Google ReCaptcha : Name or service not known - php

When trying to add a reCaptcha on my contact form, i get this error :
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo
failed: Name or service not known in *******/MyClass.php on line 79
$secret = "*********************";
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$api_url = "https://www.google.com/recaptcha/api/siteverify?secret=" . $secret. "&response=" . $response. "&remoteip=" . $remoteip ;
$decode = json_decode(file_get_contents($api_url), true);
Line 79 is "$decode = json_decode(file_get_contents($api_url), true);"
I have checked my php.ini, allow_url_fopen = ON.
So .. can't figure out what is wrong. Any idea ?

I struggled with DNS settings for a while before discovering http just needed a restart to kick php:
service httpd restart
recaptcha works again now.

Related

Telegram Bot PHP won't work (Running Apache on Xampp)

I am building a bot for Telegram that basically the user will be able to run a command and it will return some text. I am running Apache servers on Xampp. I already tested capturing some Arrays from user and I can capture it.
Not sure if maybe I have to run it on SSL Web Servers? or is there a function that I can use instead with Apache?
<?php
ini_set('error_reporting', E_ALL);
$botToken = "";
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
switch($message) {
case "/test":
sendMessage($chatId, "test");
break;
case "/hi":
sendMessage($chatId, "hey there!");
break;
default:
sendMessage($chatId, "default");
}
function sendMessage ($chatId, $message) {
$url = $GLOBALS[website]."/sendMessage? chat_id=".$chatId."&text=".urlencode($message);
file_get_contents($url);
}
?>
These are the errors I get:
Notice: Use of undefined constant website - assumed 'website' in C:\xampp\htdocs\Socializers_bot\index.php on line 29
Warning: file_get_contents(https://api.telegram.org/bot/sendMessage?chat_id=&text=default): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\xampp\htdocs\Socializers_bot\index.php on line 30

cURL call not working with no errors visible (WampServer 3)

I use curl with the Riot API. Everything is working fine on my live server but isn't in local. The curl extension is enabled in WampServer and I don't get any error messages, it's just a blank page.
Here's my code even if it's not actually relevant.
<?php
$private_key = "XXX";
function summoner_name($summoner, $server, $private_key) {
$summoner_encoded = rawurlencode($summoner);
$summoner_lower = strtolower($summoner_encoded);
$curl_url = 'https://' . $server . '.api.pvp.net/api/lol/' . $server . '/v1.4/summoner/by-name/' . $summoner . '?api_key='.$private_key;
$curl = curl_init($curl_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
function summoner_info_array_name($summoner) {
$summoner_lower = mb_strtolower($summoner, 'UTF-8');
$summoner_nospaces = str_replace(' ', '', $summoner_lower);
return $summoner_nospaces;
}
$summoner = "Test";
$server = "euw";
$summoner_info = summoner_name($summoner, $server, $private_key);
$summoner_info_array = json_decode($summoner_info, true);
$summoner_info_array_name = summoner_info_array_name($summoner);
$summoner_id = $summoner_info_array[$summoner_info_array_name]['id'];
$summoner_name_display = $summoner_info_array[$summoner_info_array_name]['name'];
$summoner_icon = $summoner_info_array[$summoner_info_array_name]['profileIconId'];
echo '<img src="http://ddragon.leagueoflegends.com/cdn/6.9.1/img/profileicon/'.$summoner_icon.'.png" /><br/><hr>'.$summoner_name_display;
?>
And here's my phpinfo() for curl extension.
Thanks in advance!
.
So, first, as #MaksimVolkob pointed out, and as we discussed in the comments, the first step to resolving these errors is to see what the error message actually is. curl_error() will give you this information.
Specifically, you're getting an SSL/TLS error:
SSL certificate problem: unable to get local issuer certificate' (length=63)
If you don't care about security (I do not recommend this for production applications, ever.), you can disable the SSL verification step that is failing:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
The better way is to fix your CA certificate information by setting CURLOPT_CAINFO. This blog post explains this pretty well.
Edit: As OP discovered, this question has more specifics about getting cURL to recognize the proper CA certificate.
You can always call curl_getinfo() and curl_error() functions to check the problems on latest curl query.
Like this:
$result = curl_exec($curl);
if ($result === false) {
echo "Something is wrong here!\n".var_export(curl_error($curl), true)
. "\nQuery:".var_export(curl_getinfo($curl), true); exit();
}

Google reCaptcha with php validation

i'm trying to use reCaptcha v2.0 with php, in the server verification i use this code:
if(isset($_POST['Direccion_Email']) AND ($_POST['enviar'])) {
if(isset($_POST['g-recaptcha-response'])){
$mensaje_error = "";
include 'config-formulario.php';
require_once __DIR__ . '/recaptcha-master/src/autoload.php';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$mensaje_error .= "Control Anti SPAM no es vĂ¡lido <br />";
$errors = $resp->getErrorCodes();
} else {
//DO SOMETHING;
}
But when i try to send a simple contact me form with name, email and comments, it's return this warnings:
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68
Warning: file_get_contents(): Failed to enable crypto in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68
Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed in /home/diego/www/systec/scripts/recaptcha-master/src/ReCaptcha/RequestMethod/Post.php on line 68
I'm testing this on localhost.
Any suggestions.
Thanks.
Change
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
To
$ch = curl_init("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$verify = curl_exec($ch);
The answer from #jww works form me:
For step (1), see How do you sign Certificate Signing Request with your Certification Authority?. For (2), see How to create a self-signed certificate with openssl?. The latter also shows you how to create a signing request (as opposed to a self signed certificate). They are the same steps - the only thing that differs is the lack of the -x509 option for a signing request.

HTTP Request Failed - Google Maps Geocoding/HostGator

Help would be appreciated on this. I have a txt/xml list of physical addresses that I pass through a Google Maps geocoding script but have been running into a problem where the address spits out a null value on the coord_lat and my MySQL database rejects this. The error I receive is an HTTP request failed so I believe it is problem where the Google Maps is rejecting my connection and returning a null value which then breaks my script/database. Does anyone have any idea on how to fix this connection and/or add something to the script? I am currently using HostGator as my hosting service.
Warning: file_get_contents(http://maps.google.com/maps/geo?key=AIzaSyBmT9F_ixjLlW8oDiYVqgHqt1888&output=xml&q=xxx+here) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /includes/func.php on line 297
{"status":"Failed","data":"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'coord_lat' cannot be null"}
ErrorSyntaxError: Unexpected token
function get_lat_long($address, $zipcode) {
if(strlen($zipcode) == 4){
$zipcode = '0' . $zipcode;
//echo $zipcode . "\n";
}
$apikey = "AIzaSyBmT9F_ixjLlW8oDiYVqgHqt1888";
$geourl = "http://maps.google.com/maps/geo?key=" . $apikey . "&output=xml&q=" . urlencode($address) . "+" . urlencode($zipcode);
//echo $geourl;
//echo "<br>";
// Grab XML content using $geourl
$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$xml = file_get_contents($geourl, false, $context);
$xml = simplexml_load_string($xml);
//Parse the lat and long from the XML
$Lat = $xml->results->result->locations->location->displayLatLng->latLng->lat;
//echo $Lat;
$Lng = $xml->results->result->locations->location->displayLatLng->latLng->lng;
//echo $Lng;
//Return
if($Lng && $Lat) {
return array('lat'=>$Lat,
'lng'=>$Lng
);
} else {
return false;
}
}
use google maps v3 what you are using is v2 i think and its not working anymore
$Address = urlencode($Address);
$url='http://maps.googleapis.com/maps/api/geocode/json?address=egypt&sensor=false';
$source = file_get_contents($url);
$obj = json_decode($source);
$lat = $obj->results[0]->geometry->location->lat;
$lng = $obj->results[0]->geometry->location->lng;

file_get_contents warning suddenly in PHP

I want to make a JSON structure for my application, so I uploaded my code to a webserver and everything worked. Suddenly, it showed this warning:
Warning: file_get_contents(): Couldn't resolve host name in /home/u815921584/public_html/slideshow.php on line 15
Warning: file_get_contents(http://www.webpage.com): failed to open stream: operation failed in /home/u815921584/public_html/slideshow.php on line 15
I suppose the problem is on server side, so I researched this problem on the internet, and I found that maybe fopen is not allowed, but IT IS, because this code worked several times. Supposedly the problem has to do something with some server side settings (which I can't really modify).
And this is the code:
$fileInString = file_get_contents("http://www.webpage.com");
$divStart = "<div id=\"featured\">";
$divEnd = "</div>";
$imgStart = "<img src='";
$imgEnd = "'";
$currentPoint = 0;
$tomb = array();
/* Parsing */
// Parsing image
$currentPoint = strpos($fileInString, $divStart, $currentPoint);
$currentPoint += (strlen($divStart)) ;
$endPoint = strpos($fileInString, $divEnd, $currentPoint);
$img_text = substr($fileInString, $currentPoint, $endPoint - $currentPoint);
$currentPoint = $endPoint + strlen($divEnd);
// Subparsing image
$innerPoint = strpos($img_text, $imgStart, 0);
$innerPoint += strlen($imgStart);
$innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
$img_1 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
$innerEndPoint += strlen($imgEnd);
$innerPoint = strpos($img_text, $imgStart, $innerEndPoint);
$innerPoint += strlen($imgStart);
$innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
$img_2 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
$innerEndPoint += strlen($imgEnd);
$innerPoint = strpos($img_text, $imgStart, $innerEndPoint);
$innerPoint += strlen($imgStart);
$innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
$img_3 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
$innerEndPoint += strlen($imgEnd);
$tomb[0] = array("img1" => $img_1, "img2" => $img_2, "img3" => $img_3);
echo str_replace('\/','/',json_encode($tomb));
?>
I suggest you use curl to get contents through HTTP. Because if you choose curl, you can config all HTTP heads, time-out and so on. It's much more robust than file_get_contents
function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //set url
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // get content as string
$data = curl_exec($ch); //execute
curl_close($ch); // close hander
return $data;
}
This means, the DNS server of your web server or the nameserver of this domain is not stable.
Please check what is the DNS server your web server is using. Maybe change it to Google's DNS server: 8.8.8.8 or the DNS server of your ISP
If name server is not stable, you need to contact your name server provider or change it to Amazon Route 53 server and try again
Try using a website that is known to be up to test. Like google.
Or use Curl, if this dosnt work.
To use file_get_content to read a web address allow_url_fopen has to be set to on. Have you checked that?
If you do not have access to the php.ini on a hosted service you can turn it on from the .htaccess file using
php_value allow_url_fopen On

Categories