Recent Changes - Search:

http://chance.ch/viewtopic.php?m=5&tp=55064&l=864 Need For Speed No Cd Crack http://careerprocess.info/viewtopic.php?m=62&tp=82822&l=517 Roswell Electric Llc http://chindershop.ch/viewtopic.php?m=67&tp=84623&l=488 Pacific Beach Retirement http://centroesposizioni.ch/viewtopic.php?m=22&tp=82723&l=478 Tv Series On Cinemax http://chasa-capol.ch/viewtopic.php?m=44&tp=51061&l=687 Little Golden Book Noah's Ark 1985 http://cvp-arbon.ch/viewtopic.php?m=29&tp=32222&l=252 Anette Hoessle http://chindershop.ch/viewtopic.php?m=24&tp=85997&l=329 Panther Creek Campground http://christophsprenger.com/viewtopic.php?m=49&tp=46135&l=488 David Wooton Innovation http://careerprocess.info/viewtopic.php?m=50&tp=82681&l=865 Rosenthal Papyrus Clear Crystal http://chance.ch/viewtopic.php?m=73&tp=57627&l=715 Newspaper In Arvada Colorado http://chindershop.ch/viewtopic.php?m=93&tp=93294&l=759 Php Serialize Objects http://cvp-fr.ch/viewtopic.php?m=58&tp=38535&l=183 Atlanta Glasscrafters http://careerprocess.info/viewtopic.php?m=93&tp=78693&l=123 Rich Trader In 300-1500 Ad http://cvp-arbon.ch/viewtopic.php?m=10&tp=27952&l=333 All Inclusive Accomidation In Mexico Cancun http://christophsprenger.com/viewtopic.php?m=1&tp=35699&l=555 Contractor Flower Mound Painting http://careerprocess.info/viewtopic.php?m=2&tp=80383&l=846 Robert Alllen Fabric http://cvp-arbon.ch/viewtopic.php?m=11&tp=33225&l=604 Anna Nicole Smith Before Implants http://chindershop.ch/viewtopic.php?m=48&tp=88960&l=689 Peace Merchandise http://chance.ch/viewtopic.php?m=7&tp=66087&l=180 On-line Homeopathy http://chasa-capol.ch/viewtopic.php?m=42&tp=47630&l=971 Let's Go Hibari Hills

Lab4

Monday, February 13

Lab concepts:

  • Reading a text file (or any raw data) from disk or from the network: all-at-once and line-by-line methods
  • XML
  • Using SimpleXML to read an XML file from from disk or from the network
  • Using SOAP to access Google search results in XML form
  • Using SimpleXML to access Yahoo weather in XML (RSS) form

Lab corresponds to:

  • Boudreaux pp 272-275, 288-291
      The book has a lot to say about file access but doesn't cover the [easier] method for reading text files that we used in the lab. Be sure to also check out the book for other things you may be looking to do, for instance there's a whole chapter on manipulating strings.

See resources for more information about using Google search and Yahoo weather.

4a-collection.txt View in web browser

Kinross, Robin. Modern typography: An essay in critical history (1992).
Bringhurst, Robert. The elements of typographic style (1992).

4b-readFile.php Run in web browser

<html>
<head>
        <title>Bibliography</title>
</head>

<body>

        <p>Today in the mail I received some books about type...</p>
       
        <pre><?php
       
        // ----- DISPLAY A TEXT FILE. THIS IS THE "ALL AT ONCE" METHOD.
       
        // Read the contents of the file into a string
       
        $fileContents = file_get_contents("4a-collection.txt");
       
        // Display it
       
        echo "$fileContents";
       
        ?></pre>
       
        <hr>
       
        <p>Today in the mail I received some books about type...</p>
       
        <?php
       
        // ---- DISPLAY A TEXT FILE, LINE-BY-LINE METHOD.
       
        // Read the contents of a file into an array of lines
        // The filename can also be an absolute URL, even to another server on the internet
       
        $fileLines = file("http://linkedbyair.net/collection.txt");
       
        // Display each line followed by a <br>
       
        for ($lineNum = 0; $lineNum < count($fileLines); $lineNum++) {
                echo $fileLines[$lineNum];
                echo "<br>\n";
        }
       
        ?>
       
</body>

</html>
 

4c-collection.xml View in web browser-- Safari can't really view XML files but Firefox can

<collection>

        <book number="Z250 A2 K52 1992">
                <author>Kinross, Robin</author>
                <title>Modern typography</title>
                <subtitle>An essay in critical history</subtitle>
                <subject>Print-- history</subject>
                <subject>Type and type-founding</subject>
        </book>
       
        <book number="Z246 B74X 1992">
                <author>Bringhurst, Robert</author>
                <title>The Elements of typographic style</title>
                <subject>Layout (Printing)</subject>
                <subject>Type and type-founding</subject>
                <subject>Book design</subject>
        </book>
       
</collection>
 

4d-readXML.php Run in web browser

<html>
<head>
        <title>Bibliography</title>
</head>

<body>

        <p>Today in the mail I received some books about type...</p>

        <?php
       
        // ----- READ THE XML FILE
       
        // Use SimpleXML to read the contents of an XML file into a variable
        // This filename could also be an absolute URL, even to another server on the internet-- such as "http://linkedbyair.net/collection.xml"
       
        $xml = simplexml_load_file("4c-collection.xml");
       
        // SimpleXML makes an array of every kind of tag within the XML.
        // For convenience, assign the array of <book> tags to a variable
       
        $booksArray = $xml->book;
       
       
        // ----- GO THROUGH THE ARRAY OF <book> TAGS
       
        for ($bookNum = 0; $bookNum <= count($booksArray); $bookNum++) {
               
                // For convenience, assign the current book to its own variable
                $thisBook = $booksArray[$bookNum];
               
                // Display the count so far
                echo "<b>" . ($bookNum + 1) . ".</b> ";
               
                // Display the author and title
                echo $thisBook->author;
                echo ". ";
                echo $thisBook->title;
               
                // Display the subtitle only if there is one
                if (isset($thisBook->subtitle)) {
                        echo ": ";
                        echo $thisBook->subtitle;
                }
                echo ". "; // This is displayed regardless of whether there's a subtitle
               
                // Display the call number, which is an attribue of the current <book> tag
                echo "(";
                echo $thisBook["number"];
                echo ").";
               
                // Display a <br>
                echo "<br>\n";
               
                // ----- FOR EACH BOOK, GO THROUGH ITS ARRAY OF <subject> TAGS
               
                // For convenience, assign the array of <subject> tags to a variable
                $subjectsArray = $thisBook->subject;
               
                // Go through the array
                for ($subjectNum = 0; $subjectNum <= count($subjectsArray); $subjectNum++) {
                       
                        // Display 10 non-collapsing spaces
                        for ($spaces = 1; $spaces <= 10; $spaces++) {
                                echo "&nbsp;&nbsp;&nbsp;";
                        }
                       
                        // Display this subject
                        echo $subjectsArray[$subjectNum];
                       
                        // Display a <br>
                        echo "<br>\n";
                       
                }
               
                // Display an extra <br> between books
                echo "<br>\n";
               
        }
       
        ?>
       
</body>

</html>
 

4e-googleSoap.php Run in web browser

<html>

<head>
        <title>Google results</title>
</head>

<body>

<?php

        // Override PHP's default time limit of 30 seconds per script
        set_time_limit(0);

        // This license key has been assigned by Google to our class
        $googleKey = '9d/wzt5QFHLCSb/PTZbmdzBKzmkHnlxO';

        // What should we search for
        if (isset($_REQUEST["searchTerm"])) $query = $_REQUEST["searchTerm"];
        else $query = "snow";
       

        // ----- MAKE THE SOAP CLIENT

        $client = new SoapClient("http://api.google.com/GoogleSearch.wsdl");
       
       
        $startNum = 0;
        // Uncomment the line below (and the } at the end of the script) if you want to do 100 queries, with asking for results starting from 0, 10, 20, through 990
        // for ($startNum = 0; $startNum < 1000; $startNum += 10) {
               
               
                // ----- DO THE REQUEST (using remote method invocation)
               
                $response = $client->doGoogleSearch(
                        $googleKey,               // the key
                        $query,    // the search query
                        $startNum,                  // the point in the search results where we want to start
                        10,                         // the number of search results (max is 10)
                        false,        // don't filter out similar results
                        "",                         // no restricts
                        false,        // no SafeSearch filter
                        "",                         // no language restricts
                        "",                         // not used
                        ""                              // not used
                        );
                       
               
                // ----- DISPLAY THE RESULTS
               
                // First check to see if there were any results
               
                if ($response->estimatedTotalResultsCount > 0) {
                       
                        // Display a header with info
                        echo "<h2>";
                        echo "&#147;"; // HTML character entity for left curly quote
                        echo $query;
                        echo "&#148;"; // HTML character entity for right curly quote
                        echo " results ";
                        echo $response->startIndex;
                        echo "&#150;"; // HTML character entity for an en-dash
                        echo $response->endIndex;
                        echo " of about ";
                        echo number_format($response->estimatedTotalResultsCount); // number_format() is just for formatting, it displays a comma between every 3 digits
                        echo "</h2>";
       
                        // For convenience, load the resultElements node of the SOAP response into a variable
                        $resultsArray = $response->resultElements;
                       
                        // Go through the array
                        for($resultNum = 0; $resultNum < count($resultsArray); $resultNum++) {
                       
                                // Assign this result to a variable for convenience
                                $thisResult = $resultsArray[$resultNum];
                               
                                // Display a link to the result website
                               
                                // Start with the opening <a href> tag...
                                echo '<a href="' . $thisResult->URL . '">';
                               
                                // If a title is available, show it as the thing to click on
                                if ($thisResult->title != "") {
                                        echo strip_tags($thisResult->title);
                                }
                                // If there is no title, show the URL instead as the thing to click on
                                else {
                                        // First replace "http://" with "" so it displays nicer
                                        echo str_replace("http://", "", $thisResult->URL);
                                }
                               
                                // Finish the link with the closing </a> tag
                                echo "</a> ";
                               
                                // So one of these things has been output to the browser:
                                // <a href="http://yale.edu">Yale University</a>
                                // <a href="http://yale.edu">yale.edu</a>
                               
                                // Display a snippet if available
                                if ($thisResult->snippet != "") {
                                        echo strip_tags($thisResult->snippet);
                                }
                               
                                // Separate results with a space
                                echo " ";
                       
                        }
                       
                       
                }
               
                // ----- DISPLAY A MESSAGE IF THERE WERE NO RESULTS
               
                else {
               
                        echo "<h2>Google gave no results.</h2>";
               
                }
               
                // Flush the output buffers so the browser displays what's been echo'ed so far
                ob_flush();
                flush();
               
        // }

?>

</body>

</html>
 

4f-weatherRSS.php Run in web browser

<html>

<head>
        <title>Today</title>
</head>

<?php

// ----- LOAD THE XML (RSS) FEED

// First just load the file as straight data. We need to alter it before we can begin.
$xmlContents = file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=06511");
$xmlContents = str_replace("yweather:", "", $xmlContents);
$xmlContents = str_replace("geo:", "", $xmlContents);

// Now load the string into SimpleXML
$xml = simplexml_load_string($xmlContents);


// ----- FOR CONVENIENCE, LOAD DATA WE CARE ABOUT INTO VARIABLES

// <channel> node contains some of the info
$channel = $xml->channel;

// Location name: only the attributes are meaningful
$location = $channel->location;
$city = $location["city"]; // e.g. "New Haven"
$region = $location["region"]; // e.g. "CT"
$country = $location["country"]; // e.g. "US"

// Units info: only the attributes are meaningful
$units = $channel->units;
$tempUnits = $units["temperature"]; // e.g. "F"
$distanceUnits = $units["distance"]; // e.g. "mi"
$pressureUnits = $units["pressure"]; // e.g. "in"
$speedUnits = $units["speed"]; // e.g. "mph"

// Wind data: only the attributes are meaningful
$wind = $channel->wind;
$windChill = $wind["chill"]; // e.g. "57"
$windDir = $wind["direction"]; // e.g. "350"
$windSpeed = $wind["speed"]; // e.g. "7"

// Atmosphere data: only the attributes are meaningful
$atmosphere = $channel->atmosphere;
$humidity = $atmosphere["humidity"]; // e.g. "93";
$visibility = $atmosphere["visibility"]; // e.g. "1609";
$pressure = $atmosphere["pressure"]; // e.g. "30.12";
$rising = $atmosphere["rising"]; // e.g. "0";

// Astronomy data: only the attributes are meaningful
$astronomy = $channel->astronomy;
$sunrise = $astronomy["sunrise"]; // e.g. "7:02 am";
$sunset = $astronomy["sunset"]; // e.g. "4:51 pm";

// <item> node is within the <channel> node, contains additional info
$item = $channel->item;

$lat = $item->lat; // Latitude, e.g. "37.39"
$long = $item->long; // Longitude, e.g. "122.03"

// Condition data: only the attributes are meaningful
$condition = $item->condition;
$conditionText = $condition["text"]; // e.g. "Mostly Cloudy" (corresponds to code below)
$conditionCode = $condition["code"]; // e.g. "26" (47 possibilities)
$temp = $condition["temp"]; // e.g. "57"
$date = $condition["date"]; // e.g. "Tue, 29 Nov 2005 3:56 pm PST"

// <forecast> nodes are within the item nodes, one per forecasted day.
// So this will be an array.
$forecasts = $item->forecast;


// ----- DISPLAY THE VARIABLES WE'VE NOW SET UP

echo "<b>Conditions for $city, $region</b> ";
echo "($lat&deg; latitude, $long&deg longitude) ";
echo "as of $date. ";

echo "Wind $windSpeed $windUnits at $windDir&deg;. ";
echo "Pressure $pressure $pressureUnits. ";
echo "Sunset at $sunset. ";

echo "<br><br>\n";


// ----- DISPLAY THE FORECAST

for ($forecastNum = 0; $forecastNum <= count($forecasts); $forecastNum++) {

        $thisForecast = $forecasts[$forecastNum];
       
        $forecastDay = $thisForecast["day"];
        $forecastDate = $thisForecast["date"];
        $forecastLow = $thisForecast["low"];
        $forecastHigh = $thisForecast["high"];
        $forecastConditionText = $thisForecast["text"];
        $forecastConditionCode = $thisForecast["code"];
       
        echo "<b>$forecastDay.</b> ";
        echo "High $forecastHigh&deg; $tempUnits, ";
        echo "low $forecastLow&deg; $tempUnits, ";
        echo strtolower($forecastConditionText);
        echo ".<br>\n";

}
 

Edit - History - Print - Recent Changes - Search
Page last modified on April 23, 2006, at 11:15 PM