2014-09-20 2 views
0

PHP로 Google API를 사용하여 통화 변환기를 만들려고하지만 비어있는 페이지를 제공합니다.USD to GBP 통화 변환기

의 index.php :

<div id='output'></div> 
<form action='convert.php' method='post'> 
Amount: <input name='amount' type='text'><br/> 
From: <select name='from'> 
     <option value='USD'>USD</option> 
     <option value='GBP'>GBP</option> 
     </select><br/> 
To: <select name='to'> 
     <option value='GBP'>GBP</option> 
     <option value='USD'>USD</option> 
     </select><br/> 
<input name='submit' type='submit' value='submit'> 
</form> 

convert.php : 새 페이지에 데이터를 게시하고 있기 때문에

<?php 

function currency($from, $to, $amount) { 
    $content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to); 
    $doc = new DOMDocument; 
    @$doc->loadHTML($content); 
    $xpath = new DOMXpath($doc); 
    $result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue; 
    return str_replace(' '.$to, '', $result); 
} 

if(!empty($_POST) && isset($_POST['submit'])) { 
    $output = currency($_POST['from'], $_POST['to'], $_POST['amount']); 
    echo "<script>document.getElementById('output').innerHTML = '<p>" . $output . "</p>'></script>"; 
} 

?> 
+0

무엇이 문제입니까? –

+0

... 어떻게 작동합니까? – James

+0

어떻게 작동하지 않습니까? 당신은 무엇을 기대하며 대신 무엇을 얻습니까? –

답변

0

그것은 빈입니다.

은 index.php를에서 당신은 당신이 출력이 끝날 것으로 예상 어디에있는 줄

<div id='output'></div> 

있습니다. 그러나 양식에 제출을 푸시하면 실제로 완전히 다른 페이지로 이동하게됩니다. URL을 보면, 이제는 으로 끝납니다. convert.php

제출을 클릭하면 페이지의 출처를 확인하면 실제 내용과 정확히 일치합니다.

그냥이 함께 output.php 라인 (14)을 대체 :

echo $output; 

당신은 다음 결과를 볼 수 있습니다.

출력이 양식이있는 페이지와 동일한 페이지에 나타날 것으로 예상했기 때문에 AJAX을 사용하는 것이 좋습니다. 당신은 구글 URL을 명중하고를 가져 오는 기능 아래의 매개 변수를 전달해야

<?php 

function currency($from, $to, $amount) { 
    $content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to); 
    $doc = new DOMDocument; 
    @$doc->loadHTML($content); 
    $xpath = new DOMXpath($doc); 
    $result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue; 
    return str_replace(' '.$to, '', $result); 
} 

if(!empty($_POST) && isset($_POST['submit'])) { 
    $output = currency($_POST['from'], $_POST['to'], $_POST['amount']); 
} 

?> 

<div id='output'><?php if(isset($output)) { echo $output; } ?></div> 
<form action='index.php' method='post'> 
Amount: <input name='amount' type='text'><br/> 
From: <select name='from'> 
     <option value='USD'>USD</option> 
     <option value='GBP'>GBP</option> 
     </select><br/> 
To: <select name='to'> 
     <option value='GBP'>GBP</option> 
     <option value='USD'>USD</option> 
     </select><br/> 
<input name='submit' type='submit' value='submit'> 
</form> 
+0

나는 그것을 index.php로 바꾸었고 양식을 제출할 때 여전히 빈 페이지가 나타납니다. – James

+0

"소스보기"를 할 경우 빈 페이지의 원본에 무엇이 있습니까? –

+0

글쎄, 이건 이상해 ... 내 웹 사이트에 iFrame과 양식을 포함하고있어 변환기가 완벽하게 작동합니다. 그러나 PHP 파일에 직접 액세스 할 때 그렇지 않습니다. 오 잘. – James

0

:

또는 다음과 같은 작업을 수행하여 동일한 페이지에 결과를 얻기를위한 논리를 둘 수 있었다 통화 변환 응답.

매개 변수 :
$from_Currency = 응답을 원합니다.
$to_Currency = 변환해야 할 대상.
$amount = 전환하려는 금액입니다.

<?php 

function get_currency($from_Currency, $to_Currency, $amount) { 

$amount = urlencode($amount); 
$from_Currency = urlencode($from_Currency); 
$to_Currency = urlencode($to_Currency); 

$url = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency"; 

$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$rawdata = curl_exec($ch); 
curl_close($ch); 
$data = explode('bld>', $rawdata); 
$data = explode($to_Currency, $data[1]); 
return round($data[0], 2); 
} 

// Call the function to get the currency converted 
echo get_currency('USD', 'INR', 1); 

?> 

응답 :

65.24 구글은 링크 즉를 업데이트했습니다

0

(USD의 현재 속도에 따라 경찰)이 새 사이트로 이동되었습니다. 그래서이 도움이 될 수 있습니다

function currencyConvert($from,$to,$amount){ 
    $url = "https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
    $request = curl_init(); 
    $timeOut = 0; 
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request); 
    curl_close($request); 

     $rawdata = str_replace('</span>','',str_replace('<span class="bld">','',$response)); 
    list(,$val) = explode("bld>",$rawdata); 
    list($raw2,) = explode("<input",$val); 
    return (float) $raw2; 
} 

이 함수를 호출하려면

echo currencyConvert('USD','NPR',1); 

이는 네팔어 루피 $ 1을 변환합니다.

USD에서 GBP로 비슷할 수 있습니다.