2014-05-17 2 views
0

몇 시간을 들여다 보았지만 왜 JSON 쿼리가 구문 분석하려고하는 CSV/TXT 파일의 마지막 줄에 대한 결과 만 반환하는지 이해할 수 없지만. 여기 CSV를 반복하고 각 결과를 사용하여 JSON 쿼리를 구문 분석

코드입니다 : 다른 사람의 아무도 돌아 오지

(가) '4066'는 RG12 0GD의 우편 번호에 대한 올바른 변수
Distance by Post Code Generator v0.1 by Phil Hughes 

RG12 0GA 
RG12 0GB 
RG12 0GC 
RG12 0GD4066 

하지만 :

//Enter API Key Here 
$api_key = 'AIzaSyB9Dq3w1HCxkS5qyELI_pZuTmdK8itOBHo'; 
$origin = 'RG12 1AA'; 
$output_type = 'json'; //xml or json 
$csv_location = 'http://www.naturedock.co.uk/postcodes.csv'; 

//Do not edit 
$base_url = 'https://maps.googleapis.com/maps/api/directions/'; 
$origin_url = '?origin='; 
$destination_url = '&destination='; 
$end_url = '&sensor=false&key='; 

$page = join("",file("$csv_location")); 
$kw = explode("\n", $page); 

for($i=0;$i<count($kw);$i++){ 
    $destination = $kw[$i]; 
    echo $destination; 
    $raw_url = $base_url . $output_type . $origin_url . $origin . $destination_url . $destination . $end_url . $api_key; 
    $request_url = str_replace(' ', '', $raw_url); 

    $getJson = file_get_contents($request_url); 
    $routes = json_decode($getJson); 

    $result = $routes->routes[0]->legs[0]->distance->value; 
    echo $result . '<br>'; 
} 

내가 얻을 결과이 같은 모습 당신이 볼 수있는 결과.

도와주세요. 귀하의

join("",file("$csv_location")); 

답변

0

모든 라인을 분리하지 않고 한 줄에 파일을 feom 연결된. 다음의 explode()는 더 이상 개행을 보지 않습니다. 따라서 한 줄로 만 작업하고 있습니다. count ($ kw)는 항상 1로 평가되고 루프는 한 번만 실행됩니다.