Jump to content

php newbie.


flemingmike

Recommended Posts

i think i might have done something simple to screw this up.

 

<?
function get_content($url)
{
   $ch = curl_init();

   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_HEADER, 0);

   ob_start();

   curl_exec ($ch);
   curl_close ($ch);
   $string = ob_get_contents();

   ob_end_clean();
  
   return $string;   
} 

header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n";
echo "<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\"\n";
echo " \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\n\n";
echo "<rss version=\"0.91\">\n\n";
echo "<channel>\n";

echo "<title>MLB Scores</title>\n";
echo "<link>http://www.mlb.com</link>\n";
echo "<description>MLB Scores</description>\n";
echo "<language>en-us</language>\n";
echo "<image>\n";
echo " <title>MLB Scores</title>\n";
echo " <url>http://www.durhamit.ca/test/mlb.gif</url>\n";
echo " <link>http://www.mlb.com</link>\n";
echo "</image>\n";
echo "<webMaster>[email protected]</webMaster>\n";

$content = get_content ("http://sports.espn.go.com/mlb/bottomline/scores");

$content_array=explode("&", $content);
$scorearray = array();
$i=0;
foreach($content_array as $content) {
if (strpos($content, "_left")) {
	$equalpos = strpos($content, "=");
	$end = strlen($content);
	$title = substr($content, ($equalpos+1), $end);
	$title = str_replace("^", "", $title);
	$title = str_replace("%20", " ", $title);
	$scorearray[$i]["title"] = $title;

}
if (strpos($content, "_url")) {
	$equalpos = strpos($content, "=");
	$end = strlen($content);
	$url = substr($content, ($equalpos+1), $end);
	$url = str_replace("^", "", $url);
	$url = str_replace("%20", " ", $url);
	$scorearray[$i]["url"] = $url;
			$i++;

}
}

foreach($scorearray as $score) {
echo "<item>\n";
echo "<title>".$score["title"]."</title>\n";
echo "<link>".$score["url"]."</link>\n";
echo "</item>\n";
}

echo "</channel>\n";
echo "</rss>\n";
?>

 

any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/202979-php-newbie/
Share on other sites

You can use the function get_file_contents instead of curl to get the information returned by the URL. Then you can use the function parse_str to parse it:

 

<?php
$content = file_get_contents('http://sports.espn.go.com/mlb/bottomline/scores');
parse_str($content,$vars);
?>

This leaves you with an array in the variable $vars which contains (as of right now)

Array
(
    [mlb_s_delay] => 120
    [mlb_s_stamp] => 0526093306
    [mlb_s_left1] => NY Yankees 0   Minnesota 0 ()
    [mlb_s_right1_count] => 0
    [mlb_s_url1] =>
    [mlb_s_left2] => Chicago Sox 0   Cleveland 0 (TOP 3RD)
    [mlb_s_right2_1] => ¢         1 out
    [mlb_s_right2_count] => 1
    [mlb_s_url2] => http://sports.espn.go.com/mlb/gamecast?gameId=300526105
    [mlb_s_left3] => Texas at Kansas City (2:10 PM ET)
    [mlb_s_right3_1] => Feldman vs. Hochevar
    [mlb_s_right3_count] => 1
    [mlb_s_url3] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left4] => Detroit at Seattle (3:40 PM ET)
    [mlb_s_right4_1] => Bonderman vs. Vargas
    [mlb_s_right4_count] => 1
    [mlb_s_url4] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left5] => NY Yankees at Minnesota (7:05 PM ET)
    [mlb_s_right5_1] => Pettitte vs. Liriano
    [mlb_s_right5_2] => Airing on ESPN
    [mlb_s_right5_count] => 2
    [mlb_s_url5] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left6] => Oakland at Baltimore (7:05 PM ET)
    [mlb_s_right6_1] => Cahill vs. Matusz
    [mlb_s_right6_count] => 1
    [mlb_s_url6] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left7] => Toronto at LA Angels (7:05 PM ET)
    [mlb_s_right7_1] => Morrow vs. Pineiro
    [mlb_s_right7_count] => 1
    [mlb_s_url7] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left8] => Pittsburgh at Cincinnati (7:10 PM ET)
    [mlb_s_right8_1] => Ohlendorf vs. Arroyo
    [mlb_s_right8_count] => 1
    [mlb_s_url8] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left9] => Philadelphia at NY Mets (7:10 PM ET)
    [mlb_s_right9_1] => Blanton vs. Takahashi
    [mlb_s_right9_count] => 1
    [mlb_s_url9] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left10] => Atlanta at Florida (7:10 PM ET)
    [mlb_s_right10_1] => Hanson vs. Robertson
    [mlb_s_right10_count] => 1
    [mlb_s_url10] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left11] => Boston at Tampa Bay (7:10 PM ET)
    [mlb_s_right11_1] => Lackey vs. Garza
    [mlb_s_right11_count] => 1
    [mlb_s_url11] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left12] => LA Dodgers at Chicago Cubs (8:05 PM ET)
    [mlb_s_right12_1] => Billingsley vs. Gorzelanny
    [mlb_s_right12_count] => 1
    [mlb_s_url12] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left13] => Houston at Milwaukee (8:10 PM ET)
    [mlb_s_right13_1] => Oswalt vs. Narveson
    [mlb_s_right13_count] => 1
    [mlb_s_url13] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left14] => Arizona at Colorado (8:40 PM ET)
    [mlb_s_right14_1] => Lopez vs. Jimenez
    [mlb_s_right14_count] => 1
    [mlb_s_url14] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left15] => St. Louis at San Diego (10:05 PM ET)
    [mlb_s_right15_1] => Garcia vs. Correia
    [mlb_s_right15_count] => 1
    [mlb_s_url15] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_left16] => Washington at San Francisco (10:15 PM ET)
    [mlb_s_right16_1] => Atilano vs. Lincecum
    [mlb_s_right16_count] => 1
    [mlb_s_url16] => http://sports.espn.go.com/mlb/scoreboard
    [mlb_s_count] => 16
    [mlb_s_loaded] => true
)

 

From this you should be able to get what you want.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/202979-php-newbie/#findComment-1063669
Share on other sites

You have to use $vars to access the data you want.

 

Ken did a print_r on the $vars to show a mock up of the possible variable you will get. If, you do not know how to use arrays or know what they are. I would highly suggest you get reading about arrays and their uses. They are essential, imo, to programming in PHP.

Link to comment
https://forums.phpfreaks.com/topic/202979-php-newbie/#findComment-1063676
Share on other sites

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.