3 Replies - 651 Views - Last Post: 27 January 2012 - 04:30 PM Rate Topic: -----

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Trouble implementing University proxy server for weather rss project

Posted 26 January 2012 - 01:32 PM

I have created a webpage which parses in weather from Yahoo feeds.

The spoken webpage is shown below

Attached Image

Everything works 100% when on my home network but I have to implement the universities proxy server so that it can be used on their network.

The lecturer has supplied us with a function for the proxy server which I have saved in a proxy.php script.

proxy.php

<?php

function get_file($uri) { 
    # get a file via ****** proxy and stop caching 
    $context = stream_context_create( 
    array('http'=> 
        array('proxy'=>'proxysg.******.ac.uk:8080', 
             'header'=>'Cache-Control: no-cache' 
           ) 
  ));   
$contents = file_get_contents($uri,false,$context); 
return $contents; 
}; 
?>


For security purposes I have hidden the university domain from the above function.

I have a cityConfig.php script for storing the URL for the weather feeds and include the proxy.php script. The feed URL's are parsed into the get_file() function from proxy.php.

cityConfig.php

<?php

require_once 'proxy.php'; 

// City 1 //
$city1 = 'London';
$city1WeatherRssUrl = get_file('http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c');

// City 2 //
$city2 = 'New York';
$city2WeatherRssUrl = get_file('http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f');

// City 3 //
$city3 = 'Paris';
$city3WeatherRssUrl = get_file('http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c');

?>


The URL is parsed into the getWeather.php script where I pull out the required tags using the get_current_weather() function. The first else clause is as far as the script gets before it dies displaying

"Feed not found"

getWeather.php

function get_current_weather($weatherRssUrl) {

// Get XML data from source
if (isset($weatherRssUrl)) {
    $feed = file_get_contents($weatherRssUrl);
} else {
    die('Feed not found');
}

$xml = new SimpleXmlElement($feed);

$weather = get_dateTime($xml);
$weather = get_temperature($xml);
$weather = get_conditions($xml);
$weather = get_icon($xml);

return $weather;
}


As mentioned above, when I remove the proxy.php script, everything works as expected. Any ideas why the $feed variable from the get_current_weather() function is not set.

To diagnose the problem I started at the cityConfig.php script and can successfully echo out the xml file when I add the the following lines of code to city1 e.g. London.

cityConfig.php

<?php

require_once 'proxy.php'; 

// City 1 //
$city1 = 'London';
$city1WeatherRssUrl = get_file('http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c');
echo $city1WeatherRssUrl;
exit();


An echo of the $feed variable set in getWeather.php script returns nothing. It's not being set.

getWeather.php

<?php

function get_current_weather($weatherRssUrl) {

// Get XML data from source
if (isset($weatherRssUrl)) {
    $feed = file_get_contents($weatherRssUrl);
    echo $feed;
    exit(); 
} else {
    die('Feed not found');
}


Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble implementing University proxy server for weather rss project

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Trouble implementing University proxy server for weather rss project

Posted 26 January 2012 - 02:21 PM

View PostNewToJava2011, on 26 January 2012 - 08:32 PM, said:

The URL is parsed into the getWeather.php script where I pull out the required tags using the get_current_weather() function. The first else clause is as far as the script gets before it dies displaying

"Feed not found"

...

Any ideas why the $feed variable from the get_current_weather() function is not set.

The "Feed not found" error would not be displayed because the $feed variable isn't set. What triggers that error is a false result from the isset($weatherRssUrl) call. All that tells you is that the URL passed into the function is not set. You never actually verify the contents of the $feed variable.


Why, in your get_current_weather function, are you using a plain get_file_contents call instead of your get_file function? Wouldn't you need to use the latter in order to get around the proxy, just like in your other calls?
Was This Post Helpful? 0
  • +
  • -

#3 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 166
  • Joined: 21-November 11

Re: Trouble implementing University proxy server for weather rss project

Posted 27 January 2012 - 02:13 PM

[quote name='Atli' date='26 January 2012 - 02:21 PM' timestamp='1327612915' post='1539259']

View PostNewToJava2011, on 26 January 2012 - 08:32 PM, said:

Why, in your get_current_weather function, are you using a plain get_file_contents call instead of your get_file function? Wouldn't you need to use the latter in order to get around the proxy, just like in your other calls?


After making the change you have suggested I'm presented with the following;

Warning: file_get_contents(http://weather.yahooapis.com/forecastrss?p=UKXX0085&u=c) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known in /Applications/XAMPP/xamppfiles/htdocs/Twinz/includes/proxy.php on line 21

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,519
  • Joined: 23-August 08

Re: Trouble implementing University proxy server for weather rss project

Posted 27 January 2012 - 04:30 PM

Reading the manual page for the valid HTTP context options, I see this:

Quote

request_fulluri boolean
When set to TRUE, the entire URI will be used when constructing the request. (i.e. GET http://www.example.c...th/to/file.html HTTP/1.0). While this is a non-standard request format, some proxy servers require it.

Defaults to FALSE.


Maybe you need to try that. Failing that, try talking to the administrators at your school to find out what they require.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1