file_get_contents重定向到302

时间:2013-04-23 07:47:34

标签: php fopen

我正在尝试使用file_get_contents访问外部网址。我正在接受 302作为响应移动。 它在我的localhost中正常工作。

 $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);

如何解决此问题?

2 个答案:

答案 0 :(得分: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中启用或不启用Chech“allow_url_fopen”

答案 1 :(得分: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);