0

I have the following code:

<HTML>
<?php

include('Net/SSH2.php');


$ssh = new Net_SSH2('my ip');
if (!$ssh->login('user', 'pass')){
  exit('Login Failed');
  }
$msisdn = $_POST['msisdn'];

$result = $ssh->exec('/usr/src/artsx/ash1.sh  '.$msisdn.'');



//echo $result; 
echo "<br>";

$array = explode(' ',$result);

//print_r($array);


$mcc = $array[0];
$mnc = $array[1];
$lac = $array[2];
$cell = $array[3];

//echo $mcc;
//echo $mnc;
//echo $lac;
//echo $cell;




$data = array(
"homeMobileCountryCode" => $mcc,
"homeMobileNetworkCode" => $mnc,
"radioType" => "gsm",
"cellTowers" => array( array(  
   "cellId" => $cell,
   "locationAreaCode" => $lac,
   "mobileCountryCode" => $mcc,
   "mobileNetworkCode" => $mnc
))
);


$url = "http://www.googleapis.com/geolocation/v1/geolocate?key=my_key";

function curl_get_contents($url)
{
 $ch = curl_init($url);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
} 

print_r($data); 

$json = json_decode(curl_get_contents($url));

$options = array(
       'http' => array(
        'header'  => "Content-Type: application/json\r\n",
        'method'  => 'POST',
        'content' => json_encode($data),
    ),
);
print_r($json);


$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = json_decode($result,true);

echo $result;

$location=$result['location'];
$lat=$location['lat'];
$lng=$location['lng'];

//echo $lat;
echo "$lat $lng\n";

?>

And the response i receive is:

Warning: file_get_contents(http://www.googleapis.com/geolocation/v1/geolocate?key=my_key): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in D:\Apache24\htdocs\lookup\glookup.php on line 81.

Any ideas what can be the reason? I've already checked allow_url_fopen, and it is set to "On".

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Did you replace `my_key` to your actual key while you running the program? Also if `file_get_contents` does not work for you, you might use cURL, you can refer to this answer: http://stackoverflow.com/a/26028500/4195406 – ztan Jan 30 '15 at 18:32
  • Hi, for sure i've replaced my_key with my actual key. As for the CURL, I've tried several versions, with no luck. – Ashot Avetisyan Feb 02 '15 at 06:58

0 Answers0