【问题标题】:Get returned array values from object into variable PHP从对象中获取返回的数组值到变量 PHP
【发布时间】:2013-06-07 09:04:27
【问题描述】:

我对 OOP PHP 还是很陌生,我已经编写了一个类来从 Google 的地图 API 获取 Lat/Long。这有点无关紧要,因为它工作正常。

从该类中,我返回一个 LAT/LONG/ADDRESS 值数组,如下所示:

    class latLngFinder {

    /**
    * Creates cURL connection to Google API v3
    * @param string $url The URL to read from
    * @return string The URL content
    */
    private function getURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tmp = curl_exec($ch);
        curl_close($ch);
        if ($tmp != false){
            return $tmp;
        }
    }

    /**
    * Get Latitude/Longitude/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    public function findCoord($address){
        $address = str_replace(' ', '+', $address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;

            // Use this to debug returned XML
            //print_r($xml);

            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = array( (string) $xml->result->geometry->location->lat);
                $lng = array( (string) $xml->result->geometry->location->lng);
                $formatted_addr = array( (string) $xml->result->formatted_address);

            }
        }
        // Return data
        return array('formatted_address' =>  $formatted_addr[0], 'lat' => $lat[0], 'lng' => $lng[0]);

    }


};

当我创建类对象时:

 // Include class
    require_once(dirname(__FILE__) . '/class.latLngFinder.php');

    // Initialise the latLngFinder class
    $obj = new latLngFinder();

    print_r($obj->findCoord($row['address']));

我想获取从类返回的数组,这就是我卡住的地方。我试过使用:

$var = print_r($obj->findCoord($row['address']), true);

但是,这会将数组作为我不想要的字符串返回。有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 你试过像 $var = $obj->findCoord($row['address']); print_r($var, true); ?
  • 是的,它仍然返回一个字符串而不是一个数组。

标签: php xml database arrays object


【解决方案1】:
<?php
class latLngFinder {
    public $output;
    public $lat;
    public $lng;
    public $formatted_address;

    public function __construct($inputaddress)
    {
        $this->address = $inputaddress;
        $this->output = $this->findCoord();
    }

    /**
    * Creates cURL connection to Google API v3
    * @param string $url The URL to read from
    * @return string The URL content
    */
    private function getURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tmp = curl_exec($ch);
        curl_close($ch);
        if ($tmp != false){
            return $tmp;
        }
    }

    /**
    * Get Latitude/Longitude/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    private function findCoord(){
        $address = str_replace(' ', '+', $this->address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;

            // Use this to debug returned XML
            //print_r($xml);

            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = $xml->result->geometry->location->lat;
                $lng = $xml->result->geometry->location->lng;
                $formatted_addr = $xml->result->formatted_address;
                $this->lat = $lat;
                $this->lng = $lng;
                $this->formatted_address = $formatted_addr;
            }
        }
    }
}

// Initialise the latLngFinder class
$obj = new latLngFinder($row['address']);
echo $obj->lat;
echo "<br />";
echo $obj->lng;
echo "<br />";
echo $obj->formatted_address;
?>

【讨论】:

  • PHP 注意:未定义属性:latLngFinder::$lat 我猜$lat 变量需要声明?我真的不知道该怎么做
  • 等一下,我忘了返回 vars 2 分钟
  • 好的,我现在没有收到错误,但屏幕上没有返回任何内容。
  • 对不起,我调用了一个不正确的函数(从我的一些代码中复制和粘贴)再次尝试稍微更新一下
  • PHP 致命错误:无法在第 60 行重新分配 $this(私有函数 findCoord($this->$address){)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多