Documentation Of PHP API

It is possible to query our server from PHP scripts. Here are the steps to follow:

  • Register for API key. If you already have a key for Javascript interface, note that a separate registration is needed for PHP interface.
  • Download the library topocoding.inc
    // Copyright (c) 2007 topocoding.com
    // This software, or any portion of it, can be only used for resolving and processing data from topocoding.com server.
    // Other use is subject to the terms and conditions of your written agreement with topocoding.com service providers.
    // If you do not have such an agreement, then such a use of this material is strictly prohibited.
    // This software is provided "as is", without express or implied warranty of any kind.
    
    // ================ Numeric functions follow =========================
    
    function topoToFeets($meters)
    {
      return $meters * 3.28084;
    }
    
    function topoComputeDistance($lat1,$lon1,$lat2,$lon2)
    {
      $lat1 = $lat1 / 180 * pi();
      $lon1 = $lon1 / 180 * pi();
      $lat2 = $lat2 / 180 * pi();
      $lon2 = $lon2 / 180 * pi();
      $dlon = $lon2 - $lon1;
      $dlat = $lat2 - $lat1;
      $a = sin($dlat/2) * sin($dlat/2) + cos($lat1) * cos($lat2) * sin($dlon/2) * sin($dlon/2);
      $c = 2 * atan2(sqrt($a), sqrt(1-$a));
      $R = 6378.137;
      return $R * $c;
    }
    
    function topoComputeIntermediate($lat1,$lon1,$lat2,$lon2,$fraction,$dist = -1.0 )
    {
      if ( $dist <= 0.0 )
        $dist = topoComputeDistance($lat1,$lon1,$lat2,$lon2);
    
      $R = 6378.137;
    
      $lat1 = $lat1 / 180 * pi();
      $lon1 = $lon1 / 180 * pi();
      $lat2 = $lat2 / 180 * pi();
      $lon2 = $lon2 / 180 * pi();
      $dist = $dist / $R;
    
      $A=sin((1-$fraction)*$dist)/sin($dist);
      $B=sin($fraction*$dist)/sin($dist);
      $x = $A*cos($lat1)*cos($lon1) +  $B*cos($lat2)*cos($lon2);
      $y = $A*cos($lat1)*sin($lon1) +  $B*cos($lat2)*sin($lon2);
      $z = $A*sin($lat1)           +  $B*sin($lat2);
      $lat=atan2($z,sqrt($x*$x+$y*$y));
      $lon=atan2($y,$x);
    
      $lat = $lat * 180 / pi();
      $lon = $lon * 180 / pi();
      return array( $lat, $lon, $dist * $fraction * $R );
    }
    
    // =========== Server access functions follow ================
    
    function topoEncodeCoordinates( $coordinates )
    {
      $result = '';
    
      $topoUrlChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!*()';
      $topoUrlCharsLength = strlen( $topoUrlChars );
      $topoUrlCharsSqrt = floor( sqrt( $topoUrlCharsLength ) );
      $topoUrlMaxCoordinates = 280;
      
      if ( sizeof( $coordinates ) > $topoUrlMaxCoordinates )
        die( 'Too many points to fit into the safe URL length limit.' );
    
      for ( $j = 0; $j < sizeof( $coordinates); $j++ )
      {
        $lat = $coordinates[ $j ][ 0 ];
        $lon = $coordinates[ $j ][ 1 ];
        if ( $lat < 0.0 )
           $lat = 180 + $lat;
        $lat = $lat / 180.0;
        $lat = $lat - floor( $lat );
        if ( $lon < 0.0 )
           $lon = 360 + $lon;
        $lon = $lon / 360;
        $lon = $lon - floor( $lon );
        for ( $i = 0; $i < 3; $i++ )
        {
           $lat = $lat * $topoUrlCharsLength;
           $index = floor( $lat );
           $lat = $lat - $index;
           $result = $result . substr( $topoUrlChars, $index, 1 );
           $lon = $lon * $topoUrlCharsLength;
           $index = floor( $lon );
           $lon = $lon - $index;
           $result = $result . substr( $topoUrlChars, $index, 1 );
        }
        $lat = $lat * $topoUrlCharsSqrt;
        $lon = $lon * $topoUrlCharsSqrt;
        $index = floor( $lat ) * $topoUrlCharsSqrt + floor( $lon );
        $result = $result . substr( $topoUrlChars, $index, 1 );
      }
      return $result;
    }
    
    
    function topoGetAltitudes( $coordinates )
    {
       global $topoKey;
       $url = 'http://topocoding.com/api/altitude_v1.php?id=x&key=' . $topoKey . '&l=' . topoEncodeCoordinates( $coordinates );
       $content = '';
       if ( ini_get( 'allow_url_fopen' ) == '1' )
       {
         $content = file_get_contents( $url );
       }
       else if ( function_exists( 'curl_init' ) )
       {
         $ch = curl_init();
         curl_setopt ( $ch, CURLOPT_URL, $url );
         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
         curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
         $content = curl_exec( $ch );
         curl_close( $ch );
       }
       else
       {
         // see also http://www.php-mysql-tutorial.com/php-tutorial/php-read-remote-file.php
         die( 'No method to read from remote server was found.' );
       }
       if ($content !== false) {
         $tmp0 = explode( '[', $content );
         if ( $tmp0[ 0 ] == 'topoCall(x,' )
         {
           $tmp1 = explode( ']', $tmp0[ 1 ] );
           return explode(',',$tmp1[ 0 ]);
         }
         else
         {
           die( $content );
         }
       } else {
         die( 'Unable to resolve altitudes.' );
       }
    } 
  • Create your own PHP script which includes topocoding.inc library. Use the function topoGetAltitudes($places) to get the altitudes of places in the array. Each place itself is also an array composed of real-numbered latitude and longitude. At most 280 places can be resolved at once. An array of integer altitudes in meters is returned. Here is a full source code of the simplest example for resolving the altitude of 2 hardcoded places. Modify the API key in this file to use your own registered key.

    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Using topocoding API in PHP</title>
    </head>
    <body>
    
    <?php
    
      $topoKey = 'your_key_comes_here';
    
      include( 'topocoding.inc' );
    
      // now the main test program begins
      // altitudes of 2 hardcoded places are resolved
    
      print_r( topoGetAltitudes( array( array( 50.5678, 17.1234 ), array( 49.3456, 16.6789 ) ) ) );
    
    ?>
    
    </body>
    </html>

Please note that the free PHP API will be subject of the same bandwidth and count limitations as the free Javascript API, for commercial users we offer dedicated servers.

©2012 TOPOCODING.com