0

Is it possible to pull text data from another domain (not currently owned) using php? If not any other method? I've tried using Iframes, and because my page is a mobile website things just don't look good. I'm trying to show a marine forecast for a specific area. Here is the link I'm trying to display.

Update...........

This is what I ended up using. Maybe it will help someone else. However I felt there was more than one right answer to my question.

<?php

$ch = curl_init("http://forecast.weather.gov/MapClick.php?lat=29.26034686&lon=-91.46038359&unit=0&lg=english&FcstType=text&TextType=1");

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
echo $content;

?>
Justin Petty
  • 283
  • 4
  • 21

3 Answers3

1

Try using a user-agent as shown below. Then you can use simplexml to parse the contents and extract the text you want. For more info on simplexml.

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-agent: www.example.com"
  )
);

$content = file_get_contents($url, false, stream_context_create($opts));
$xml = simplexml_load_string($content);
icanc
  • 3,499
  • 3
  • 35
  • 45
1

This works as I think you want it to, except it depends on the same format from the weather site (also that "Outlook" is displayed).

<?php
//define the URL of the resource
$url = 'http://forecast.weather.gov/MapClick.php?lat=29.26034686&lon=-91.46038359&unit=0&lg=english&FcstType=text&TextType=1';
//function from http://stackoverflow.com/questions/5696412/get-substring-between-two-strings-php
function getInnerSubstring($string, $boundstring, $trimit=false)
{
    $res = false;
    $bstart = strpos($string, $boundstring);
    if($bstart >= 0)
    {
        $bend = strrpos($string, $boundstring);
        if($bend >= 0 && $bend > $bstart)
        {
            $res = substr($string, $bstart+strlen($boundstring), $bend-$bstart-strlen($boundstring));
        }
    }
    return $trimit ? trim($res) : $res;
}
//if the URL is reachable
if($source = file_get_contents($url))
{
    $raw = strip_tags($source,'<hr>');
    echo '<pre>'.substr(strstr(trim(getInnerSubstring($raw,"<hr>")),'Outlook'),7).'</pre>';
}
else{
    echo 'Error';
}
?>

If you need any revisions, please comment.

ionFish
  • 1,004
  • 1
  • 8
  • 20
0

You may use cURL for that. Have a Look at http://www.php.net/manual/en/book.curl.php

Rob
  • 1,158
  • 1
  • 12
  • 22