Jump to content

Can this be simplified?


zimsabre

Recommended Posts

Hello eveyone, i tryed making a script that would find a figure on a certain website, then print it on mine.

 

Here is the script..

 

<?php

//Just change the item name inside the "" to change what item to price check!!!

    $item = strtolower("blue dragon scale");

    $itemfixed = str_replace( " ", "+", $item );

 

    function parseString( $start, $end, $string )

    {

        $before = explode( $start, $string );

        $return = explode( $end, $before[1] );

        return $return[0];

    }

 

        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_URL, 'http://services.runescape.com/m=itemdb_rs/results.ws?query='.$item );

        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

        $contents = strtolower( curl_exec ( $ch ) );

        curl_close ( $ch );

 

        $price = parseString( "$item</a></td>\n<td>", "</td>", $contents );

        echo "".$price."Gp";

?>

 

and to bring the item price up on my page i would use

<?php require('../prices/bluedragscalesprice.php'); ?>

 

But for every item im checking id use a new .php of the script with a different item name saved.

Can i just have one script with a different input method on my web page?

 

Thanks

Zimsabre.

Link to comment
https://forums.phpfreaks.com/topic/189333-can-this-be-simplified/
Share on other sites

function parseString( $start, $end, $string ) {
  $before = explode( $start, $string );
  $return = explode( $end, $before[1] );
  return $return[0];
}

function checkPrice($itemName) {
  $item = strtolower("blue dragon scale");
  $itemfixed = str_replace( " ", "+", $item );

  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, 'http://services.runescape.com/m=itemdb_rs/results.ws?query='.$item );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  $contents = strtolower( curl_exec ( $ch ) );
  curl_close ( $ch );
  $price = parseString( "$item</a></td>\n<td>", "</td>", $contents );
  return $price
}

echo checkPrice('blue dragon scale').'Gp';

 

 

 

 

No you can just move both functions to a separate file, and include them when needed. You can use them like this:

 

require('checkprice.php');

echo 'Blue Dragon Scale: '.checkPrice('blue dragon scale').'Gp';
echo 'Red Dragon Scale: '.checkPrice('red dragon scale').'Gp';
echo '10 x Blue Dragon Scale: '.(10 * checkPrice('blue dragon scale')).'Gp';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.