Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Is possible to grab value from webpage?

    Hello everyone,
    I would like to ask you for a help - I am doing one project in php (calculating of cocoa price in my currency) and I am trying to catch price from this site (link: URL) but I'm not successful with PHP Simple HTML DOM Parser, unfortunatelly

    I do not know what I am doing wrong because I had managed how to obtain the current value of the USD/EUR from ECB but here it does not work...

    Therefore, I want to ask - is this task possible and if so, is anyone so good to help me? (sorry for my bad english)

    Regards...
    S.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,530
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Maybe not the most elegant ... but this is my shot at it:

    Code:
    <?php
    				
    $url="http://www.fxstreet.com/rates-charts/related-rates/?id=commodities+%28futures%29%%3bcocoa+futures";
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    
    $lines=explode("\n",$result);
    
    foreach($lines as $line){
    	if (strpos($line,"last_")){
    	list($junk,$item)=explode(">",$line);
    	list($last,$junk)=explode("<",$item);
    	}
    	if (strpos($line,"high_")){
    	list($junk,$item)=explode(">",$line);
    	list($high,$junk)=explode("<",$item);
    	}
    	if (strpos($line,"low_")){
    	list($junk,$item)=explode(">",$line);
    	list($low,$junk)=explode("<",$item);
    	}
    }
    
    echo "Last: ".$last."<br />";
    echo "High: ".$high."<br />";
    echo "Low: ".$low."<br />";
    
    ?>

    The output:

    Last: 2,965.00
    High: 2,977.00
    Low: 2,945.00

    .

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts
    example only using PHP Simple HTML DOM Parser

    PHP Code:
    $page 'http://www.fxstreet.com/rates-charts/related-rates/?id=commodities+%28futures%29%3baluminum+futures%3bcotton+futures%3bgasoline+futures%3blead+futures%3bbrent+crude+oil+futures%3bdiesel+futures%3bnatural+gas+futures%3bgold+futures%3bheating+oil+futures%3bcoffee+futures%3bcocoa+futures%3bcopper+futures%3bcorn+futures%3bnickel+futures%3bpalladium+futures%3bplatinum+futures%3bsilver+futures%3bsoybeans+futures%3bwheat+futures%3bwti+crude+oil+light+futures%3bzinc+futures%3bsugar+futures+'

    $html->load_file($page);


    $data = array();
    foreach(
    $html->find('tr') as $tr) {
        foreach(
    $tr->find('td[class*=col],td[id*=last_],td[id*=open_],td[id*=high_]') as $td)
            
    $data[] = preg_replace('/[^a-z]/i''-'$td->id).$td->plaintext;
    }
    foreach (
    $data as $line){
    echo 
    $line .'<br />';


    // output
    aluminum futures
    last---------1,833.00
    open---------1,813.50
    high---------1,833.00
    cotton futures
    last---------91.00
    open---------90.25
    high---------91.23
    gasoline futures
    last---------3.04
    open---------3.03
    high---------3.06
    lead futures
    last---------2,085.50
    open---------2,085.50
    high---------2,085.50
    brent crude oil futures
    last---------109.72
    open---------109.18
    high---------110.35
    diesel futures
    last---------925.50
    open---------918.50
    high---------927.00
    natural gas futures
    last---------4.53
    open---------4.54
    high---------4.60
    gold futures
    last---------1,301.45
    open---------1,300.08
    high---------1,302.08
    heating oil futures
    last---------3.01
    open---------2.99
    high---------3.01
    coffee futures
    last---------187.10
    open---------192.15
    high---------199.40
    cocoa futures
    last---------2,965.00
    open---------2,951.00
    high---------2,977.00
    copper futures
    last---------6,600.00
    open---------6,545.00
    high---------6,600.00
    corn futures
    last---------497.75
    open---------502.25
    high---------505.00
    nickel futures
    last---------17,875.00
    open---------17,609.00
    high---------17,875.00
    palladium futures
    last---------801.75
    open---------801.25
    high---------802.35
    platinum futures
    last---------1,436.25
    open---------1,435.25
    high---------1,437.75
    silver futures
    last---------19.60
    open---------19.55
    high---------19.61
    soybeans futures
    last---------1,517.25
    open---------1,510.50
    high---------1,522.75
    wheat futures
    last---------687.00
    open---------700.00
    high---------710.75
    wti crude oil light futures
    last---------103.76
    open---------103.01
    high---------104.10
    zinc futures
    last---------2,056.00
    open---------2,031.00
    high---------2,058.00
    Last edited by low tech; 04-17-2014 at 07:53 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    884
    Thanks
    173
    Thanked 101 Times in 101 Posts
    To get just cocoa (not sure id will always be same though)


    PHP Code:
    foreach($html->find('tr[id=52559003]') as $tr) {
        foreach(
    $tr->find('td[class*=col],td[id*=last_],td[id*=open_],td[id*=high_]') as $td)
            
    $data[] = preg_replace('/[^a-z]/i''-'$td->id).$td->plaintext;
    }
    foreach (
    $data as $line){
    echo 
    $line .'<br />';

    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •