0

I am trying to access the exrernal url using file_get_contents. And I am getting 302 moved as the response. It was working fine in my 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);

How to resolve this issue?

borrible
  • 17,120
  • 7
  • 53
  • 75
B L Praveen
  • 1,812
  • 4
  • 35
  • 60
  • Ah **please** don't give us unknown variables ! – HamZa Apr 23 '13 at 07:49
  • but still I am getting the 302 moved even after adding follow_location(false) to header – B L Praveen Apr 23 '13 at 07:55
  • Are you sure your final URL is correct? I can't tell what your final URL is supposed to look like but are you sure you shouldn't be having `&` symbols between each variable to separate the $_REQUEST variables sent? As it stands to me it looks like the q variable you are sending is `100USD=?GBP` all squished together which may or may not be right. – aug Apr 23 '13 at 08:18
  • Yes.. I ran successfully in localhost – B L Praveen Apr 23 '13 at 08:20
  • `allow_url_fopen` should be set to 1 at your server. if it is set, you can try set `user_agent` to something using `ini_set`. refer to [here](http://www.php.net/manual/en/function.file-get-contents.php#95665) – Amir Apr 23 '13 at 08:49

2 Answers2

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

I got result

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

Chech "allow_url_fopen" is enabled or not in php.ini

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • I checked the allow_url_open has well .....when I tried to echo ini_get('allow_url_fopen') I got value 1 – B L Praveen Apr 23 '13 at 08:36
  • I want to know is this a server issue..Server is not allowing to access the external url.. – B L Praveen Apr 23 '13 at 08:41
  • `allow_url_fopen` can not be set using `ini_set` – Amir Apr 23 '13 at 08:46
  • @amir But I am getting the value 1 to allow_url_fopen it means it is already set – B L Praveen Apr 23 '13 at 08:55
  • @BLPraveen,CURL is a better option for you,If your server have issues – Shijin TR Apr 23 '13 at 09:04
  • @shin I am getting the same response in curl as well.. – B L Praveen Apr 23 '13 at 09:08
  • HTTP/1.0 302 Found X-Frame-Options: SAMEORIGIN Location: http://www.google.com/ig/calculator?hl=en&q=110USD%3D%3FGBP Cache-Control: private Content-Type: text/html; charset=UTF-8 Set-Cookie: PREF=ID=d05ab279ab90dd61:TM=1366711081:LM=1366711081:S=LxK0Fm_pahW7X11W; expires=Thu, 23-Apr-2015 09:58:01 GMT; path=/; domain=.google.com X-Content-Type-Options: nosniff Date: Tue, 23 Apr 2013 09:58:01 GMT Server: igfe Content-Length: 260 X-XSS-Protection: 1; mode=block I am not able understand this is 302 redirect response I am getting.... – B L Praveen Apr 23 '13 at 10:00
0

I dont know what was the wrong with my previous code But this worked to me

    $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);
B L Praveen
  • 1,812
  • 4
  • 35
  • 60