Hello I use this curl for site http://ip-whois.net but if I debug in my local comp I harcode $ip = "176.241.128.140"; - this my ip that I was given by my provider and I have $out = false, why ???
echo curl_error ($curl); = "Empty reply from server". I try this site http://www.ip-whois.net/ in browser and not work, what are the analogy this site who work with curl_setopt($curl, CURLOPT_URL, 'http://ip-whois.net/ip_geo.php?ip='.$ip); ?
how to do this right ?:
$ip = $request->getClientIp();//I hard code $ip = "176.241.128.140"
$record = $hAid->getInfoIpCity($ip);
if ( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, 'http://ip-whois.net/ip_geo.php?ip='.$ip);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
$out = curl_exec($curl);
$matches = array();
$country = preg_match_all("/Страна: (.*)/i", $out, $matches);
print_r($matches[1][1]);
curl_close($curl);
}
Related
I would like to make a small Fortnite API, but I always get an error in the JSON file.
{"message":"Invalid authentication credentials"}
My PHP Code:
$ch = curl_init();
//pc, xbl, psn
curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/pc/MyName");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'TRN-Api-Key: My-API-Code'
));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$fp = fopen("myStats.json", "w");
fwrite($fp, $response);
fclose($fp);
$data = json_decode(file_get_contents("myStats.json"));
$solo = $data->stats->p2;//solos data
$duos = $data->stats->p10;//duos data
$squads = $data->stats->p9;//squads data
$matches = $data->recentMatches;//match data
$sesh1 = $matches[0]->id->valueInt;
$solo_wins = $solo->top1->valueInt;
$duos_wins = $duos->top1->valueInt;
$squads_wins = $squads->top1->valueInt;
$solo_matches = $solo->matches->valueInt;
$duos_matches = $duos->matches->valueInt;
$squads_matches = $squads->matches->valueInt;
$solo_kd = $solo->kd->valueDec;
$duos_kd = $duos->kd->valueDec;
$squads_kd = $squads->kd->valueDec;
$solo_games = $solo->matches->valueInt;
$duos_games = $duos->matches->valueInt;
$squads_games = $squads->matches->valueInt;
$solo_kills = $solo->kills->valueInt;
$duos_kills = $duos->kills->valueInt;
$squads_kills = $squads->kills->valueInt;
$total_matches = ($solo_matches+$duos_matches+$squads_matches);
$total_wins = ($solo_wins+$duos_wins+$squads_wins);
$total_kills = ($solo_kills+$duos_kills+$squads_kills);
$total_kd = (round($total_kills/($total_matches-$total_wins),2));
echo 'Total Matches: '.$total_matches.'<br>';
echo 'Total Wins: '.$total_wins.'<br>';
echo 'Total Kills: '.$total_kills.'<br>';
echo 'Total KD: '.$total_kd.'<br>';
echo $sesh1;
?>
I entered the correct API code. Why is this message written in a JSON file and not my wins? It's so crazy because it should work.
What returns on line 11? You are using var_dump($response);
Could you write die(); under that line and provide us the return that it gives?
There might be something wrong on how you handle the headers. I can't check for sure right now as I am not home. But I might be able to test this out later.
I'm trying to use the simple weather API, but somehow it doesn't recognize it as an object whatever I do. This is my code:
$url = "https://www.amdoren.com/api/weather.php?api_key=za8LEJ8F9mcHK8SvLxdM98rM9mNFjW&lat=40.7127837&lon=-74.0059413";
$curl = curl_init($url);
$curl_response = curl_exec($curl);
$jsonobj = json_decode($curl_response);
$msg = "Temperature in ".$city."will be: ". $jsonobj->forecast->max_c;
and this is the data I'm trying to reach with $jsonojb->forecast->max_c:
{
"error" : 0,
"error_message" : "-",
"forecast":[
{"date":"2016-12-02",
"avg_c":8,
"min_c":5,
"max_c":11,
"avg_f":46,
"min_f":41,
"max_f":52,
(...)
but it doesn't work. What am I doing wrong guys?
forecast is an array, so you have to use like this:
$forecast = $jsonobj->forecast;
$forecast[0]->max_c;
You can try out this code:
$url = "https://www.amdoren.com/api/weather.php?api_key=za8LEJ8F9mcHK8SvLxdM98rM9mNFjW&lat=40.7127837&lon=-74.0059413";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
$jsonobj = json_decode($curl_response);
$msg = "Temperature in ". $city . " will be: ". $jsonobj->forecast[0]->max_c;
echo $msg;
Hope it helps!
The "forecast" is an array, you have to add [0] to get the first one and then you will be able to get your "max_c".
Also, your API does not give you the city. You would have to use the Google Geocoding API to convert the lon,lat into a City Name.
$api_key = "za8LEJ8F9mcHK8SvLxdM98rM9mNFjW";
$lon = -74.0059413;
$lat = 40.7127837;
$url = "https://www.amdoren.com/api/weather.php?api_key=".$api_key."&lat=".$lat."&lon=".$lon."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
$max_c = $response['forecast'][0]['max_c'];
// Your API doesn't return the city name.
$city = "City Name";
$msg = "Temperature in ".$city."will be: ". $max_c;
echo $msg;
To get your City Name, here's the function you can use. You will need to replace the API KEY.
// Geocode
function geocode($lat,$lon){
$details_url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lon."&key=YOUR_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
// If Status Code is ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED or INVALID_REQUEST
if ($response['status'] != 'OK') {
return null;
}
$formatted_address = $response['results'][0]['formatted_address'];
$geometry = $response['results'][0]['geometry'];
$longitude = $geometry['location']['lat'];
$latitude = $geometry['location']['lng'];
$array = array(
'lat' => $geometry['location']['lng'],
'lon' => $geometry['location']['lat'],
'location_type' => $geometry['location_type'],
'formatted_address' => $formatted_address
);
return $array;
}
I am trying to get 10 pages result listed using the following cod below. When i run the URL directly i get a json string but using this in code it does not returns anything. Please tell me where i am doing wrong.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
print_r($json);
Now i am using the following code but it outputs only four entries of a page. Please tell me where i am doing wrong.
$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
{
$term = $val.' site:.edu';
$query = urlencode($term);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
//print_r($json);
foreach($json['responseData']['results'] as $data)
{
echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
}
}
Just try with urlencode
$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';
I have a problem with google's geocoding api:
I have an address which should be converted to latitude and longitude by google geocoding api using cURL. You will see my code below. It worked fine, however suddenly it stopped working and I got an "OVER_QUERY_LIMIT" answer. I looked it up and it normally happens if api is requested more than 2500 times a day. This is impossible, because my website is just upon finishing and has about 20-40 geocode requests per day.
So what is really the problem that "OVER_QUERY_LIMIT" occurs? Is something wrong with my code that somehow google blocks it?!
ini_set('allow_url_fopen', true);
$CookiePath = 'mycookiepath/geocookie.txt';
$userAgent = "mysite.com";
$ListURLRoot = "http://maps.googleapis.com/maps/api/geocode/json";
$ListURLSuffix = '&sensor=false';
$Curl_Obj = curl_init(); // Setup cURL
curl_setopt($Curl_Obj, CURLOPT_COOKIEJAR, $CookiePath);
curl_setopt($Curl_Obj, CURLOPT_USERAGENT, $userAgent);
curl_setopt($Curl_Obj, CURLOPT_HEADER, 0);
curl_setopt($Curl_Obj, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($Curl_Obj, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($Curl_Obj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl_Obj, CURLOPT_TIMEOUT, 30);
curl_setopt($Curl_Obj, CURLOPT_POST, 0);
$stAddr = str_replace(' ','+', $ast);
$City = str_replace(' ','+', $ac);
$address = "$stAddr,+$City,+{$aco},+{$ap}";
$address = str_replace(' ', '+', $address);
$ListURL = "{$ListURLRoot}?address=$address$ListURLSuffix";
curl_setopt ($Curl_Obj, CURLOPT_URL, $ListURL);
$output = curl_exec ($Curl_Obj);
GetLocation($output);
function GetLocation($output) {
global $db_connect;
$Loc = json_decode($output, true);
if(isset($Loc)) {
$i = 1;
while ($Loc['status']=="OVER_QUERY_LIMIT") {
if ($i > 14) {
echo "Geocode failed!";
exit();
}
sleep(1);
$i++;
}
if(isset($Loc['status']) && stristr($Loc['status'], 'OK')) {
if(isset($Loc['results'][0]['geometry']['location'])) {
$Lat = $Loc['results'][0]['geometry']['location']['lat'];
$Lng = $Loc['results'][0]['geometry']['location']['lng'];
}
}
else {
error_log($Loc['status'], 0);
echo "Unknown error occured!";
exit();
}
}
}
Thanks in advance!
Mostly it's a Google problem. Try to avoid server side geocodeing. Instead use a client side javascript solver.
I am using PHP. I want to fetch all gmail contacts of a user, i am using a PHP code that is calling google API through CURL. But, when i am doing this on localhost, it is doing well and giving me all contacts.
But when i am doing this on online server that server in US, it is giving me response "Account Disabled" and also receiving a security mail by same user from google.
i am using below code :
function getGmailContacts($user, $password) {
//========================================== step 1: login ===========================================================
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$returns = array();
foreach (explode("\n",$result) as $line)
{
$line = trim($line);
if (!$line) continue;
list($k,$v) = explode("=",$line,2);
$returns[$k] = $v;
}
curl_close($curl);
//echo "<pre>";
//print_r($returns);exit;
if(!isset($returns['Error'])) {
//========================== step 2: grab the contact list ===========================================================
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";
$header = array(
'Authorization: GoogleLogin auth=' . $returns['Auth'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feed_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result, true);
//echo "<pre>";
//print_r($data);exit;
$contacts = array();
$i=0;
foreach ($data['feed']['entry'] as $entry)
{
//echo $i." ";
$entryElement = $entry;
if(isset($entryElement['gd$email'])) {
$gdEmailData = $entryElement['gd$email'][0];
//$contact->title = $entryElement['title']['$t'];
//$contact->email = $gdEmailData['address'];
$contacts[$gdEmailData['address']] = $entryElement['title']['$t'];
}
}
//var_dump($contacts);
//print_r($contacts);
return $contacts;
}
else {
if($returns['Error']=='BadAuthentication') {
//echo '<strong>User Name and Password is incorrect.</strong>';
$errorArr = array("Error"=>"User Name and Password is incorrect.");
//print_r($errorArr);
return $errorArr;
}
}
}
That mail contains that
"Someone recently tried to use an application to sign in to your
Google Account....Location: New York NY, New York, United States....."
.
I thing this error is occurring from location change.
can any one help me please? Thanks in advance.