0

我正在尝试使用 file_get_contents 访问外部 url。我得到了 302 作为响应。它在我的本地主机中运行良好。

 $to_currency= 'GBP';
 $from_currency = 'USD';
 $amount= 100;
 $urlarg = "hl=en&q=$amount$from_currency%3D%3F$to_currency";
 file_get_contents("http://google.com/ig/calculator?".$urlarg);

如何解决这个问题?

4

2 回答 2

0

我不知道我以前的代码有什么问题但这对我有用

    $url = "http://www.google.com/ig/calculator?hl=en&q=100USD=?GBP";
    $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);
    $Currency_Rate = curl_exec($ch);
    curl_close($ch);
于 2013-04-23T10:39:33.553 回答
0
 <?php
 ini_set('allow_url_fopen',1);
 $to_currency= 'GBP';
 $from_currency = 'USD';
 $amount= 100;
 $urlarg = "hl=en&q=$amount$from_currency%3D%3F$to_currency";
 $result=file_get_contents("http://google.com/ig/calculator?".$urlarg);
 print_r($result);
 ?>

我得到了结果

 {lhs: "100 U.S. dollars",rhs: "65.3936699 British pounds",error: "",icc: true}

在 php.ini 中检查“allow_url_fopen”是否启用

于 2013-04-23T08:20:34.987 回答