« Projects list

Quick links

Apple actions

A friend of mine, Apple fan, asked to me to make a small script to show the Apple actions on his Website. He gave me a script that was developped by another friend and woow, so dirty code it was.

Original script

Take a look (original file):

<?

$web_site='http://wu.apple.com/fq/applewidgets/quote.asp?key=tHisIsApplewidgeTs&symbols=AAPL';
$info = 'PCTCHANGE';

function http_get( $url ) {
  $request = fopen( $url, "rb" );
  $result = "";

  while( !feof( $request ) ) {
    $result .= fread( $request, 8192 );
  }

  fclose( $request );

  return $result;
}


$chars = preg_split('/\n/', http_get( "$web_site" ));

foreach ($chars as $value) {
  if ( ereg(".*$info.*", $value) ){
    $value = ereg_replace("</*$info>","", $value);
    print ("AAPL: $value");
  }
}


?>

The code is awful and not optimized for this use which needs something really fast and simple.

Top

Final script

Now, compare with mine:

<?php
/**
 *    ###########
 *    #__________#
 *    __________#
 *    ________#
 *    _____###_____²xiT development
 *    _________#
 *    ___________#
 *    #__________#
 *    _#________#
 *    __#______#
 *    ____####
 *  
 *  appleActions() This function return the current Apple market rate
 *  
 *  @author Joris Berthelot <admin@eexit.net>
 *  @copyright Copyright (c) 2008, Joris Berthelot
 *  @version 1.0
 *  @return string //The Apple's rate
 *  @license http://www.opensource.org/licenses/mit-license.php MIT Licence
 */

function appleActions() {
    
$xml = new DOMDocument();
    
$xml->load('http://wu.apple.com/fq/applewidgets/quote.asp?key=tHisIsApplewidgeTs&symbols=AAPL');
    
$xp = new DOMXPath($xml);
    return 
$xp->query('//*/PCTCHANGE')->item(0)->nodeValue;
}

// Use:
echo '<p>Apple actions: ' appleActions() . '</p>';
?>

I'm sure there is another way to obtain the information faster than this script again. Maybe using SimpleXML, I guess.

Unfortunately, the URL is broken then the script does not work anymore. If you know the new one, be kind to forward me it.

Top

Open Source Licence

Open source

Apple actions uses the MIT Licence.

Copyright © 2008-2018, Joris Berthelot.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Top