-1

これは私が実行しようとしている単純なコードですが、データを出力しません。誰か助けてもらえますか?ありがとう!!

<?php

$addr = "Hotels in ottawa canada";
$a = urlencode($addr);
$geocodeURL = 
"https://maps.googleapis.com/maps/api/geocode/json?address=$a&sensor=false&key=my key";

$ch = curl_multi_init($geocodeURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_multi_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$geocode = json_decode($result);

$lat = $geocode->results[$i]->geometry->location->lat;
$lng = $geocode->results[$i]->geometry->location->lng; 
echo $formatted_address = $geocode->results[$i]->formatted_address;
echo $geo_status = $geocode->status;
echo $location_type = $geocode->results[0]->geometry->location_type;
echo $location_type = $geocode->results[$i]->geometry->premise;
echo $street_address = $geocode->results[$i]->street_address;


?>

ソーシャルクラシファイド

4

1 に答える 1

0

複数のアドレスをGoogleAPIと並行して渡し、結果を1つとして解析する場合は、それを実行する小さな例をまとめて、curlマルチコードを修正します。それが役に立てば幸い。

<?php 
$urls = array(
'http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true',
'http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode('Hotels').'&sensor=true');

$response = curl_get_multi($urls);

//Handle response array
foreach($response as $json){
    $result = json_decode($json);
    //echo $result->status.' ';

    //Iterate each result
    foreach($result->results as $res){
        //lat
        echo $res->geometry->location->lat.' ';
        //lng
        echo $res->geometry->location->lng.' ';
        //formatted_address
        echo $res->formatted_address.' ';
        //location_type
        echo $res->geometry->location_type.' ';
        echo '<br/>';
    }
    echo '<hr />';
}

/**
 * Curl multi function
 *
 * @param Array $urls
 * @return Array
 */
function curl_get_multi($urls) {
    $curly = array();
    $result = array();

    $mh = curl_multi_init();
    foreach ($urls as $id=>$url) {
        $curly[$id] = curl_init();
        curl_setopt($curly[$id], CURLOPT_URL,            $url);
        curl_setopt($curly[$id], CURLOPT_HEADER,         0);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curly[$id], CURLOPT_TIMEOUT,        2);
        curl_setopt($curly[$id], CURLOPT_USERAGENT,      'CurlRequest');
        curl_setopt($curly[$id], CURLOPT_REFERER,        $url);
        curl_setopt($curly[$id], CURLOPT_AUTOREFERER,    true);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);

        curl_multi_add_handle($mh, $curly[$id]);
    }
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);
    foreach($curly as $id => $c) {
        $result[$id] = curl_multi_getcontent($c);
        curl_multi_remove_handle($mh, $c);
    }
    curl_multi_close($mh);
    return $result;
}
?>
于 2012-07-06T02:28:32.180 に答える