954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pull info from gas price websites?

Anybody know if there's a way to pull gas prices from any of those gas price websites, so I can display them on my own page? I didn't know if there's a way to tap in to their source or if they provide some sort of way to do that. Reason is I'm writing a little app and I would like to have the gas prices of a couple local stations used in some calculations in my app. An example of one of those sites would be www.gasbuddy.com .

Oh, FYI, the reason I posted this in the PHP forum is that I will be using PHP to write the app.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Here are a couple of tutorials:
http://blogoscoped.com/archive/2004_06_23_index.html

http://www.devnewz.com/devnewz-3-20041221UsingPHPCURLLibrarytoScrapetheInternet.html

It's just a matter of obtaing the HTML from a request and parsing out the data you need.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Hm, good info. I'll take a look into it and let you know how it goes. This just might work. Thanks!

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

It is possible to use fopen() in PHP5 to send a POST request and obtain the content in a variable, check this tutorial:
http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

According to the amount of data, sometimes is more fast and useful to use only string functions, with a combination of strpos() and substr(); other method is using regular expressions.
http://www.webcheatsheet.com/php/regular_expressions.php
http://www.snipwiki.com/snips/show/38_find-a-substring-between-two-other-substrings

With larger files or complex patterns is recommended to use SimpleXML functions (PHP5), like is described in the first tutorial suggested by Ezzaral. Xpath would be a good improvement to locate elements easily.

Some sites have clean XML results and search API easily obtainable by public, with some limitations, like the Kayak API to search flights in tourism sites. Really, I don’t know if there is a such solution for your industry, some sites contain manuals and examples to implement search and get results on their sites.

martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23
 

Thanks for that reply. I will be simply pulling one number every time the script is run. A specific gas price. I'll look into your ideas as well.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Interested also. Let me know how this works out. Thanks.

MKIII
Newbie Poster
20 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This code gets the today's gas price
It's easy to get it on this site because the price is placed between a span with the id "GBPS_lblUSToday"

$arr = file("http://www.gasbuddy.com/");

$gasprice = "";
foreach ($arr as $key => $line)
{
    if (strstr($line, "GBPS_lblUSToday"))
    {
        $gasprice=$line;
        break;
    }
}

echo strip_tags($gasprice);


If you need any help,don't hesitate to ask me :P

Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
 

Eko I like your code, but can anyone tell me how you would do this for pulling from multiple lines with the same span id?

Many Thanks

ha1e

ha1e
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You