dimanche 22 novembre 2015

How to spit out a headline, photo(s) and story from news article using PHP

Ok so I am fairly lost and frustrated, I've been reading and working on this for hours and when I feel like I make progress...I find out I really haven't.

Problem:Take a news article from some place like cnn.com and make a php web application that spits out the headline, the story, and any photos. Make sure to strip all HTML tags. Must use classes, constructors, and CANNOT use 3rd party libraries, native PHP is ok.

So here is the article I am working on http://ift.tt/1QNEHwJ

Where I left last from frustration is this, it pulls out everything I need, but leaves the raw code showing too.

$url='http://ift.tt/1QNEHwJ';

$element = file_get_contents($url);
echo strip_tags($element , "<h1>, <img>, <p>");

I had this at one point as well

//Class Structure
class ThisIsMyClass {
    public $aHTMLContent = 'HTMLContent';
    public $aHeader = 'Header';
    public $aPictures = 'Pictures';
}

$ThisIsMyClass = new ThisIsMyClass;


//Title For Page
$element = 'Header'
$ThisIsMyClass->$element=file_get_contents("http://www.cnn.com/tech", 20);
preg_match("/<title>(.*)</title>/i", $html, $match);
$title = $match[1];
echo $element . '<br>';

//HTML Content
$element2 = 'HTMLContent'
$ThisIsMyClass->$element2=getHTML("http://www.cnn.com/tech");
echo $element2 . '<br>';

//Get all images from page
$element3 = 'Pictures';
foreach($element2->find"img" as $element3)
echo $element3->src . '<br>';

?>

I see a ton of "use curl" but since curl is 3rd party I cannot use it. I have seen pulling the title specific, but the actual title is in a meta tag. I followed a couple examples on here for extracting meta tags, but I get errors that apparently no other post gets.

Please help! I need to finish this tonight, I have done my due diligence on it and I just do not know where else to look.




Aucun commentaire:

Enregistrer un commentaire