• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file_get_contents() and CI 3.0.0

#1
when i was using file_get_contents() with CI 2, it was working as it should be as for the external pages (such as some_site.com) as for the internal pages (such as site_on_ci2.com), but with CI 3... for external file_get_contents() pages still works, for the intenal pages works but in a strange way, var_dump() shows that it contains the dom object, but when i try to fetch the elements, i get null, pls advice me how to fix it, thank u
Reply

#2
Isn't file_get_contents() a PHP function, and not a CI function?
Reply

#3
(06-12-2015, 04:47 AM)Avenirer Wrote: Isn't file_get_contents() a PHP function, and not a CI function?

yes, it's php function, but it doesn't parse the pages of the site written in CI 3.0.0, so is there any "defence" from it? can i override it somehow?
Reply

#4
file_get_contents() only retrieves a file as a STRING. It doesn't "parse" or "execute" it. It's up to you what to do with the string. So after you retrieve a "page" using file_get_contents(), what are you trying to do with it? What are the contents of the file?
Reply

#5
(06-12-2015, 07:06 AM)CroNiX Wrote: file_get_contents() only retrieves a file as a STRING. It doesn't "parse" or "execute" it. It's up to you what to do with the string. So after you retrieve a "page" using file_get_contents(), what are you trying to do with it? What are the contents of the file?

this is my parse function

PHP Code:
function parse($link)
 
   {
 
       $link    preg_match('/^https?:\/\//i'$link) ? $link 'http://'.$link;
 
       $url     parse_url($link);
 
       $url     $url['scheme'].'://'.$url['host'];
 
       $img_url $url;

 
       $link = @file_get_contents($link);

 
       $dom = new DOMDocument();
 
       @$dom->loadHTML($link);

 
       $link = new stdClass;
 
       $link->url $url;

 
       $titles $dom->getElementsByTagName('title');

 
       if ($titles)
 
       {
     
       $title $titles->item(0);
 
       }

 
       if (isset($title))
 
       {
     
       $link->title $title->nodeValue;
 
       }

 
       $meta $dom->getElementsByTagName('meta');

 
       if ($meta)
 
       {
 
           foreach ($meta as $elem)
 
           {
 
               if ($elem->getAttribute('property') == 'og:url')
 
               {
     
               $link->url $elem->getAttribute('content');
 
               }

 
               if ($elem->getAttribute('name') == 'description')
 
               {
     
               $link->description $elem->getAttribute('content');
 
               }

 
               if ($elem->getAttribute('property') == 'og:image')
 
               {
     
               $link->img $elem->getAttribute('content');
 
               }
 
           }
 
       }

 
       if ( ! isset($link->img))
 
       {
 
           $imgs $dom->getElementsByTagName('img');

 
           if ($imgs->length)
 
           {
     
           $img $imgs->item(rand(0$imgs->length 1))->getAttribute('src');
 
           }

 
           if (isset($img))
 
           {
     
           $link->img preg_match("/http/"$img) ? $img $img_url.$img;
 
           }
 
       }

 
       return $link;
 
   

this function works for the external pages but for the internal pages if only i am using 2.0, if i am using 3.0, it doesn't work
Reply

#6
You might start by removing all of the @'s from the function calls, which supress any error messages. You may be getting errors but not allowing yourself to view them?

Anything in your servers error logs? Php logs?
Reply

#7
Is the $link passed to this function same in CI2 and CI3?
Reply

#8
(06-12-2015, 12:21 PM)CroNiX Wrote: You might start by removing all of the @'s from the function calls, which supress any error messages. You may be getting errors but not allowing yourself to view them?

Anything in your servers error logs? Php logs?

no error is shown, neither with @ nor without, no error log, there is no error at all, the result is i get the dom object (var_dump returns object stdclass with length and children), but if i try to call its child, it returns null
Reply

#9
(06-12-2015, 12:49 PM)gadelat Wrote: Is the $link passed to this function same in CI2 and CI3?

yeah, they are, the function might be everywhere, even at classic php without any framework, and it will parse all sites perfectly including CI 2 but excluding CI 3
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.