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.

Recommended Answers

All 7 Replies

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

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.

commented: good info +3

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.

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

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 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.