Google maps response's language issue - php

I'll try to be short: if you need more info, I'll tell you.
I'm using this code to get infos from Google Maps:
<?php
function getData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = 'http://maps.google.com/maps/geo?output=xml&q=' . urlencode($startPlace);
$url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&gl=IT&address=' . urlencode($startPlace);
$xml = simplexml_load_string($this->getData($this->url)) or die("Error loading xml data");
$points = $xml->Response->Placemark->Point->coordinates;
$provincia = $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->SubAdministrativeAreaName;
$regione =$xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
echo $regione."<br>";
preg_match_all("/-*[0-9.]*(?=,)/", $points[0], $matches);
$longitude = $matches[0][0];
$latitude = $matches[0][2];
The code is used to retrieve infos about italian locations and till three days ago, all worked fine, but this morning I saw something strange: $regione returned by code ($xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;) had an english name.
Let's say the location found be a little town in Lombardia (where 'Lombardia' is the name of the Administrative Area), the Administartive Area name returned by Google Maps was no more 'Lombardia' but 'Lombardy'.
Since this data is used to search in a local database other places in the Administrative area and since the name used in the database is obviously italian name, application doesnìt work anymore.
I'll be grateful for any advice

The problem is solved using a different url, specifying language parameter:
'http://maps.google.com/maps/api/geocode/xml?sensor=false&language=IT&address=' . urlencode($startPlace);
This url type return correct results but defferently formed so it is necessary change the code to access the infos and put them into variables, but this solved my problem

Related

How to put URL string in json Viber API php

I am a newbie developer trying to learn web development. I am currently working on this project where articles from a website get shared automatically to a viber public chat. I am facing this problem where I cannot put the URL in the media. I think this is because its json. I am not sure what I am doing wrong here. I have included.
<?php
$Tid = "-1";
if (isset($_GET['id'])) {
$Tid = $_GET['id'];
}
$url = 'https://chatapi.viber.com/pa/post';
$jsonData='{
"auth_token":"4750f56f26a7d2ed-f6b44b76f03d039a-9601b6c9d0d46813",
"from": "K9/C2Vz12r+afcwZaexiCg==",
"type":"url",
"media": "$thisID"
// I want to use $thisID as shown above. But when I
do so this error appears [ {"status":3,"status_message":"'media' field value is not a valid url."} ]
// When I use any full form url like https://google.com it works fine
}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
?>
This would work as you are using a single quoted literal.
"media": "' . $thisID . '"
But you are always better to make a PHP array or Object and then use json_encode() to create the JSON String like this
$obj = new stdClass;
$obj->auth_token = "4750f56f26a7d2ed-f6b44b76f03d039a-9601b6c9d0d46813";
$obj->from = "K9/C2Vz12r+afcwZaexiCg==";
$obj->type = 'url';
$obj->media = $thisID;
$jsondata = json_encode($obj);
RESULT of echo $jsondata;
{"auth_token":"4750f56f26a7d2ed-f6b44b76f03d039a-9601b6c9d0d46813",
"from":"K9\/C2Vz12r+afcwZaexiCg==",
"type":"url",
"media":"-1"
}

I want to show current GOLD RATE from Gold Price India Site using PHP

Just want to show current gold rate from Gold Price India (http://www.goldpriceindia.com/) using PHP.
I have done to get data using file_get_contents() method. But its working on localhost but not in server. I want this on my FTP server too.
My Code:
$url1 = 'http://www.goldpriceindia.com/gold-price-kolkata.php';
$content1 = file_get_contents($url1);
$first_step1 = explode( '<div class="prc">' , $content1 );
$gold_rate1 = explode("</div>" , $first_step1[1] );
I am using PHP, I hope my question is clear if not I ready to explain again.
Thank You.
may be your server disabled URL file access may be you can try alternate solution to get file content.
Alternate method to get file content
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
100% working Code
preg_match('#Gold price today in India <b>\(Rs\/10gm\)</b> is <b>([0-9\.]+)#', file_get_contents('http://www.marketonmobile.com/gold_price_india.php'), $matches);
echo 'The price is: '.$matches[1];

php - xml data parsing

I am working on getting some xml data into a php variable so I can easily call it in my html webpage. I am using this code below:
$ch = curl_init();
$timeout = 5;
$url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/define?key=0b03f103-f6a7-4bb1-9136-11ab4e7b5294";
$definition = simplexml_load_file($url);
echo $definition->entry[0]->def;
However my results are: .
I am not sure what I am doing wrong and I have followed the php manual, so I am guessing it is something obvious but I am just not understanding it correctly.
The actual xml results from that link used in cURL are visible by clicking the link below , I did not post it because it is rather long:
http://www.dictionaryapi.com/api/v1/references/sd3/xml/test?key=9d92e6bd-a94b-45c5-9128-bc0f0908103d
<?php
$ch = curl_init();
$timeout = 5;
$url = "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/define?key=0b03f103-f6a7-4bb1-9136-11ab4e7b5294";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch); // you were missing a semicolon
$definition = new SimpleXMLElement($data);
echo '<pre>';
print_r($definition->entry[0]->def);
echo '</pre>';
// this returns the SimpleXML Object
// to get parts, you can do something like this...
foreach($definition->entry[0]->def[0] as $entry) {
echo $entry[0] . "<br />";
}
// which returns
transitive verb
14th century
1 a
:to determine or identify the essential qualities or meaning of
b
:to discover and set forth the meaning of (as a word)
c
:to create on a computer
2 a
:to fix or mark the limits of :
b
:to make distinct, clear, or detailed especially in outline
3
:
intransitive verb
:to make a
Working Demo

PasteBin API bug? not allowing simple post

I'm trying to make a php api request to simply make a simple paste to pastebin and i found an example at http://pastebin.com/api and it's pretty strait forward so i didnt think there would be any troubles. But the example doesn't seem to be working. I keep getting the response
Bad API request, invalid api_option
But you can clearly see it sets up api_option=paste in the string it creates...
and in the documentation it says
Creating A New Paste, [Required Parameters]
Include all the following POST parameters when you request the URL:
1. api_dev_key - which is your unique API Developers Key.
2. api_option - set as 'paste', this will indicate you want to create a new paste.
3. api_paste_code - this is the text that will be written inside your paste.
Leaving any of these parameters out will result in an error.
So....i figured it looks right, besides its the example they provided.
Anyone have any ideas what is going on here?
<?php
$api_dev_key = '1234'; // your api_developer_key
$api_paste_code = 'some random text to test'; // your paste text
$api_paste_private = '0'; // 0=public 1=private
$api_paste_name = 'savelogtest'; // name or title of your paste
$api_paste_expire_date = '10M';
$api_paste_format = 'php';
$api_user_key = ''; // if invalid key or no key is used, the paste will be create as a guest
$api_paste_name = urlencode($api_paste_name);
$api_paste_code = urlencode($api_paste_code);
$url = 'http://pastebin.com/api/api_post.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'api_option=paste&api_user_key='.$api_user_key.'&api_paste_private='.$api_paste_private.'&api_paste_name='.$api_paste_name.'&api_paste_expire_date='.$api_paste_expire_date.'&api_paste_format='.$api_paste_format.'&api_dev_key='.$api_dev_key.'&api_paste_code='.$api_paste_code.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response = curl_exec($ch);
echo $response;
?>
The API example works fine. I just ran your code (of course changed the $api_dev_key, and it worked first time. Output: http://pastebin.com/eyn9tWNS
Try and add this at the top of your script:
error_reporting(E_ALL);
ini_set("display_errors", "on");
It should give you some better error report of what is going on.

Can I Send URL with Parameters via PHP and retrieve the data?

I'm starting to help a friend who runs a website with small bits of coding work, and all the code required will be PHP. I am a C# developer, so this will be a new direction.
My first stand-alone task is as follows:
The website is informed of a new species of fish. The scientific name is entered into, say, two input controls, one for the genus (X) and another for the species (Y). These names will need to be sent to a website in the format:
http://www.fishbase.org/Summary/speciesSummary.php?genusname=X&speciesname=Y&lang=English
Once on the resulting page, there are further links for common names and synonyms.
What I would like to be able to do is to find these links, and call the URL (as this will contain all the necessary parameters to get the particular data) and store some of it.
I want to save data from both calls and, once completed, convert it all into xml which can then be uploaded to the website's database.
All I'd like to know is (a) can this be done, and (b) how difficult is it?
Thanks in advance
Martin
If I understand you correctly you want your script to download a page and process the downloaded data. If so, the answers are:
a) yes
b) not difficult
:)
Oke... here some more information: I would use the CURL extension, see:
http://php.net/manual/en/book.curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
I used a thing called snoopy (http://sourceforge.net/projects/snoopy/) 4 years a go.
I took about 500 customers profiles from a website that published them in a few hours.
a) Yes
b) Not difficult when have experience.
Google for CURL first, or allow_url_fopen.
file_get_contents() will do the job:
$data = file_get_contents('http://www.fishbase.org/Summary/speciesSummary.php?genusname=X&speciesname=Y&lang=English');
// Отправить URL-адрес
function send_url($url, $type = false, $debug = false) { // $type = 'json' or 'xml'
$result = '';
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
} else {
if (($content = #file_get_contents($url)) !== false) $result = $content;
}
if ($type == 'json') {
$result = json_decode($result, true);
} elseif ($type == 'xml') {
if (($xml = #simplexml_load_file($result)) !== false) $result = $xml;
}
if ($debug) echo '<pre>' . print_r($result, true) . '</pre>';
return $result;
}
$data = send_url('http://ip-api.com/json/212.76.17.140', 'json', true);

Categories