-1

Okay so I've been working on this little script which is bascially scrapping a page from ted.com everything works as i want it to (meaning I can print out all values that I am interested in), The problem is for some reason i get these warning when running the scraper but am unsure as to why as the values the notice / warning occer on are printed out properly

 PHP Warning:  dom_import_simplexml() expects parameter 1 to be object, null given in /var/www/ted/import_ted.php on line 23
 PHP Notice:  Trying to get property of non-object in /var/www/ted/import_ted.php on line 23
 PHP Notice:  Undefined offset: 1 in /var/www/ted/import_ted.php on line 25
 PHP Notice:  Trying to get property of non-object in /var/www/ted/import_ted.php on line 27

and this is my actually php script (I've commented lines where warning's and Notice)

<?php
    $mysqli = mysqli_connect("localhost", "user", "password", "database");
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

 $html = file_get_contents('http://www.ted.com/talks/quick-list?sort=date&order=desc');
 $doc = new DOMDocument();
 $doc->loadHTML($html);
 $sxml = simplexml_import_dom($doc);
 $rows = $sxml->xpath('//tr');
 $description="not_available";
 $ted_link="none";
 $i=0;
 //$stmt = $mysqli->prepare("INSERT INTO `ted` VALUES( ?, ?, ?, ?, ?, ?, ?)");

 foreach($rows as $row) {
    $video = Array();
    $video['pub_date']=  $row->td[0];
    $video['event'] = $row->td[1];
    $sec_temp = explode(":" , dom_import_simplexml($row->td[2])->textContent );//line23
    $video['speaker'] = $sec_temp[0];
    $video['title'] = $sec_temp[1]; //line 25
    $video['duration'] = $row->td[3];
    $video['link'] = $row->td[4]->a[2]['href']; //line27
    print( "\n  line Number: " . $i . "title: " . $video['title']);
    print ("link: " .$video['link']);
   if($i != 0){
      //      $stmt->bind_param("sssssss", $video['event'], $video['speaker'], $video['title'], $description, $ted_link, $video['link'], $description, $video['pub_date'] );
 //        $stmt->execute();
   }
  $i++;
}

// $stmt->close();

?>

Okay so Like I said Everything prints out what I am expecting including $video['title'] which generates an undefined offset for some reason. The problem is until I can make these variable "objects" I cannnot bind them as parameters to mysqli query. However I can't seem to figure out how to do this?

also incase relevent here is a snippet of a table row incase this is the problem (which I do not think it is )

<tr>
    <td>Jun 2013</td>
<td>TEDGlobal 2013</td>
<td><a href="/talks/manal_al_sharif_a_saudi_woman_who_dared_to_drive.html">Manalal-Sharif: A Saudi woman who dared to drive</a> </td>
<td>14:16</td>
<td><a href="http://download.ted.com/talks/ManalAlSharif_2013G-light.mp4?apikey=TEDDOWNLOAD">Low</a> | <a href="http://download.ted.com/talks/ManalAlSharif_2013G.mp4?apikey=TEDDOWNLOAD">Regular</a> | <a href="http://download.ted.com/talks/ManalAlSharif_2013G-480p.mp4?apikey=TEDDOWNLOAD">High</a></td>
</tr>

Note also I have tried using settype($var, "object") before binding with no luckeither (though reurned true)

Anyways any help with how I can get this to work would be greatly appreciated!

hakre
  • 193,403
  • 52
  • 435
  • 836
brendosthoughts
  • 1,683
  • 5
  • 21
  • 38

1 Answers1

1
<?php 

 $html = file_get_contents('http://www.ted.com/talks/quick-list?sort=date&order=desc');
 $doc = new DOMDocument();
 $doc->loadHTML($html);
 $sxml = simplexml_import_dom($doc);
 $rows = $sxml->xpath('//tr');
 /* print_r($rows);
 die(); */


 $description="not_available";
 $ted_link="none";
 $i=0;
 //$stmt = $mysqli->prepare("INSERT INTO `ted` VALUES( ?, ?, ?, ?, ?, ?, ?)");

 foreach($rows as $row) {

    //first object is th not an td
    if(isset($row->th))
    {
        echo $row->th[1]->a;
        echo $row->th[2]->a;        
        echo $row->th[3]->a;        
        echo $row->th[4];       

    }else{

        $video['pub_date']=  $row->td[0];
        $video['event'] = $row->td[1];
        $sec_temp = explode(":" , $row->td[2]->a);//line23
        $video['speaker'] = $sec_temp[0];
        $video['title'] = $sec_temp[1]; //line 25
        $video['duration'] = $row->td[3];
        $video['link'] = $row->td[4]->a[2]['href']; //line27
        print( "\n  line Number: " . $i . "title: " . $video['title']);
        print ("link: " .$video['link']); 

        if($i != 0){
            //      $stmt->bind_param("sssssss", $video['event'], $video['speaker'], $video['title'], $description, $ted_link, $video['link'], $description, $video['pub_date'] );
            //        $stmt->execute();
        }
        $i++;
    }

}
Sundar
  • 4,580
  • 6
  • 35
  • 61