Dev Shed Forums - PHP Development http://forums.devshed.com/ PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm. en Fri, 25 May 2018 22:47:14 GMT vBulletin 60 http://forums.devshed.com/images/misc/rss.png Dev Shed Forums - PHP Development http://forums.devshed.com/ Need Php Code Explanation http://forums.devshed.com/php-development/980672-php-code-explanation-new-post.html Wed, 23 May 2018 17:52:57 GMT Php Lovers, I opened this thread to paste codes from tutorials that I do not understand so you nice folks can clear things out. Look at this code from here: Create Simple Web Crawler Using PHP And MySQL (http://talkerscode.com/webtricks/create-simple-web-crawler-using-php-and-mysql.php) PHP: --------- // Database Structure Php Lovers,

I opened this thread to paste codes from tutorials that I do not understand so you nice folks can clear things out.
Look at this code from here:
Create Simple Web Crawler Using PHP And MySQL

PHP Code:

// Database Structure 
CREATE TABLE 'webpage_details' (
 'link' text NOT NULL,
 'title' text NOT NULL,
 'description' text NOT NULL,
 'internal_link' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
 $main_url
="http://samplesite.com";
 
$str file_get_contents($main_url);
 
 
// Gets Webpage Title
 
if(strlen($str)>0)
 {
  
$str trim(preg_replace('/\s+/'' '$str)); // supports line breaks inside <title>
  
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
  
$title=$title[1];
 }
    
 
// Gets Webpage Description
 
$b =$main_url;
 @
$url parse_url$b );
 @
$tags get_meta_tags($url['scheme'].'://'.$url['host'] );
 
$description=$tags['description'];
    
 
// Gets Webpage Internal Links
 
$doc = new DOMDocument
 @
$doc->loadHTML($str); 
 
 
$items $doc->getElementsByTagName('a'); 
 foreach(
$items as $value
 { 
  
$attrs $value->attributes
  
$sec_url[]=$attrs->getNamedItem('href')->nodeValue;
 }
 
$all_links=implode(",",$sec_url);
 
 
// Store Data In Database
 
$host="localhost";
 
$username="root";
 
$password="";
 
$databasename="sample";
 
$connect=mysql_connect($host,$username,$password);
 
$db=mysql_select_db($databasename);

 
mysql_query("insert into webpage_details values('$main_url','$title','$description','$all_links')");

?>

Now, I need to learn what these lines mean and so care to explain ?
PHP Code:

// Gets Webpage Title
 
if(strlen($str)>0)
 {
  
$str trim(preg_replace('/\s+/'' '$str)); // supports line breaks inside <title>
  
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
  
$title=$title[1];
 } 

Q1. I do no understand the BOLD part. What is this array $title and the key "1" doing here ?
// Gets Webpage Title
if(strlen($str)>0)
{
$str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
$title=$title[1];
}


Q2.
PHP Code:

// Gets Webpage Internal Links
 
$doc = new DOMDocument
 @
$doc->loadHTML($str); 

Q2a. Is this line a reference like a pointer to the DOMDocument ?
$doc = new DOMDocument;

Q2b. What is this line saying ?
@$doc->loadHTML($str);
Is it saying: Load the webpage's html and extract the links. And, find the code (regex and all) to extract the links from the the DOMDocument file.
It is similar to the "include". Right ?

If I understood wrong then care to explain these 2 lines as I don't understand them.


Q3. The following line only extracts the content from the <meta description>:
PHP Code:

@$tags get_meta_tags($url['scheme'].'://'.$url['host'] ); 

What if I want to extract the content from the <meta keywords> tag aswell ? How do I change this line ? To what do I change it to ? We need to see a sample snippet from you for our learning purpose.


Thank You! :) ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/php-code-explanation-980672.html
How To Open More Than One Webpage Simultaneously In The Background With many Threads http://forums.devshed.com/php-development/980671-webpage-simultaneously-background-threads-new-post.html Wed, 23 May 2018 17:12:25 GMT Php Foks, When we used to build bots (.exe) with Ubot, we could open many threads in the background that download many pages simultaneously. Guessing our ram was enough to open 100 pages in the background. Cannot php do the same ? Open 100 threads and get cURL to load 100 pages in the background simultaneously so the user only sees one page loading on his screen while the other 99 are out... Php Foks,

When we used to build bots (.exe) with Ubot, we could open many threads in the background that download many pages simultaneously.
Guessing our ram was enough to open 100 pages in the background.
Cannot php do the same ? Open 100 threads and get cURL to load 100 pages in the background simultaneously so the user only sees one page loading on his screen while the other 99 are out of sight.
When you submit your url to my SE (searchengine), it will first crawl the link you submitted and when it finds more links on the page like 50 then I want to get it to open 50 threads in order to load all those 50 pages simultaneously in the background (to save time) and scrape their contents (like meta tags, links, etc.).
Open many pages simultaneously or visit many links simultaneously (as you would put it) for spidering purpose.

And so, do you fine folks mind showing us newbies one code snippet on how to open many threads and another snippet to show us how to load many pages in the background out of the user's site to scrape the pages ?
And finally, the last snippet that does the 2 things mentioned above ?

Thanks! :) ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/webpage-simultaneously-background-threads-980671.html
If you had to rate it from 1-100, what would you go for? Code optimization part 1 http://forums.devshed.com/php-development/980661-rate-1-100-code-optimization-1-a-new-post.html Wed, 23 May 2018 00:10:02 GMT Hi;

I have been testing code and it seems to be ok but there's a good chance I be wrong :D

Not sure what's a good M-usage or load time.

MEMORY USAGE 2,519,960 bytes

Loading Time: Base Classes 0.0081
Controller Execution Time 0.0506
Total Execution Time 0.0590



I upload the full report ====> here <====

It is not perfect, some duplicate queries etc.

I still want to make sure this there is no series issues with this way of retrieving data.

I pass things between queries and classes quite often. Like this

PHP Code:

public function validate($launch_id,$redirect null)    
        {
            
$this->launch_library->is_valid_launch($launch_id);
            if(
$redirect==1)
                {
                    
$status $this->page_access_library->is_broadcast_open($launch_id);
                    
/* 
                        is_broadcast_open returns 1,2 or 3
                            1 = Open
                            2 = Closed
                            3 = Will Open Soon
                    */
                    
if($status==1)
                        {
                            
$redirect_url $this->launch_library->launch_sales_page($launch_id);
                        }
                    if(
$status==2)
                        {
                            
$redirect_url $this->launch_library->offer_closed_page($launch_id);
                        }
                    if(
$status==3)
                        {
                            
$redirect_url base_url('coming-soon/'.$launch_id);
                        }
                    
redirect($redirect_url);             ;
                }    
            return 
$this->data['link'] = base_url("bc-redirect/".$launch_id."");
        } 

page_access_library

PHP Code:

public function is_broadcast_open($launch_id)
              {
                 
/*
                1 = Open
                2 = Closed
                3 = Will Open Soon
                */
                
$CI =& get_instance();
                
$CI->launch_library->is_valid_launch($launch_id);
                
$query $CI->db->get_where('launch_launches', array('id' => $launch_id));
                
$results $query->result_array()[0];

                
$CI->custom_functions->set_timezone($results['launch_timezone']);
                
                
$difference strtotime('now') - strtotime($results['offer_close_date']." ".$results['offer_close_time']);
                if(
$difference 0)
                  {
                    
$return 1;
                  }
                else 
                  {
                    
$return 2;
                  }
                
$difference strtotime('now') - strtotime($results['offer_open_date']." ".$results['offer_open_time']);  
                if(
$difference 0)
                  {
                    
$return 3;
                  }
                return 
$return;  
              } 

So yes, performance-wids, if you had to rate it from 1-100, what would you go for?

++++++++++++++++++++++++++++++++++++++++++++++++++++

Edits:

1 - Note, my goal is not perfection, it's robustness.
2 - In the case of the duplicate queries, they seem to happen since the classes are not tighet to each other. Mostly happen for validation. If valid item for example. Just to experiment, I made a loop with 500 queries. Not much change in the load time and prtofiler details!
3 - Does it make a BIG difference to update the code and change let's say 4 queirs to 1? or not worth the headache since ech query takes 0.0001 seconds.
4 - I underdstand Requinex is not fully for using strtotime like this.

Tanx ]]>
PHP Development English Breakfast Tea http://forums.devshed.com/php-development-5/rate-1-100-code-optimization-1-a-980661.html
Number function !? http://forums.devshed.com/php-development/980660-function-new-post.html Tue, 22 May 2018 21:41:42 GMT Is there any function that will display a number as 60+ if the result its equal to 60 or higher but lower than 65 and if its egual to 65 or higher but lower than 70 to display 65+ ?
I'm not talking about this number in particular, but in general for all numbers. I would like to count my article views like this not like 60,61,62,63 .... 70 x viewed.
Like an interval from 5 to 5 and so on. ]]>
PHP Development pwpx2 http://forums.devshed.com/php-development-5/function-980660.html
Pass vars in URL http://forums.devshed.com/php-development/980656-pass-vars-url-new-post.html Tue, 22 May 2018 00:51:47 GMT I have done a way to subscriptors so they confirm their subscription. I have made an email stating "push YES to confirm" and push NO to unsubscribe". YES and NO contain links as
Code:

www.mysite.com/myscript.php?userId=123&answer=yes
My PHP script simply reads those vars and modifies a table in the database. Well, quite simple.
My problem is that not all answers reach my PHP so not all subscriptors can confirm or deny their subscription.
I ask, do you know any issue when sending URLs?
I am sending this question to some 10.000 email addresses and I already have got some 2.000 answers so it means my script is ok.
I'll appreciate any suggestion. ]]>
PHP Development Pere http://forums.devshed.com/php-development-5/pass-vars-url-980656.html
Redirect to log in page if come back to an idle page http://forums.devshed.com/php-development/980655-redirect-log-page-idle-page-new-post.html Mon, 21 May 2018 23:36:39 GMT Hi;

It's simple. User logs in, does things, and end up happily ever after.

There is a scenario I wanna fix.

User logs in, goes to some edit page, fills the form, but doesn't save. He goes to take a shet but comes back after an hour. Session has expired and if he/she submits the form, they get redirected to log in page.

What would be the best way to prevent that from happening? I am not too sure about autosave. Takes too long for me to do and I haven't done before.

This is the login function just for the sake of it.

PHP Code:

public function log_yser_in()
        {
            
$sql "SELECT id, email FROM launch_owners WHERE email = ?";
            
$query_log $this->db->query($sql, array($_POST['email']));
            if(
$query_log->num_rows()==1)
                {
                    if(
strlen($query_log->result_array()[0]['secret_key'])!=10)
                        {
                            
$this->generate_secret_key($_POST['email']);
                        }
                    
$_SESSION['user_id']=$query_log->result_array()[0]['id'];
                    
$_SESSION['email']=$query_log->result_array()[0]['email'];
                    
redirect(base_url('dashboard'));
                }
            
redirect(base_url('login'));    
        } 

Thanks ]]>
PHP Development English Breakfast Tea http://forums.devshed.com/php-development-5/redirect-log-page-idle-page-980655.html
Error: Undefined Index http://forums.devshed.com/php-development/980654-error-undefined-index-new-post.html Mon, 21 May 2018 22:52:47 GMT Php Folks, This is tiring! I have my db details like this: db: crawler_index tbl: crawler_index Tbl Columns: id, time_and_date,url,title,description,internal_url. The following script extracts links and indexes to the mysql tbl. Php Folks,

This is tiring!
I have my db details like this:
db: crawler_index
tbl: crawler_index

Tbl Columns: id, time_and_date,url,title,description,internal_url.

The following script extracts links and indexes to the mysql tbl.
Problem is, I keep getting this nagging error:
Notice: Undefined index: description in C:\xampp\htdocs\cURL\crawler_test.php on line

Error referencing this line:
PHP Code:

$description $tags['description']; 

Context:
PHP Code:

 // Gets Webpage Description
 
$b =$main_url;
 @
$url parse_url$b );
 @
$tags get_meta_tags($url['scheme'].'://'.$url['host'] );
 
$description $tags['description']; 

Full Code:
PHP Code:

<?php

/* 
// Database Structure 
CREATE TABLE 'webpage_details' (
 'link' text NOT NULL,
 'title' text NOT NULL,
 'description' text NOT NULL,
 'internal_link' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

*/

//Connect to Mysql Database in this input format: "Server", "MySql User", "Mysql User Password", "MySql Database Name".
$conn mysqli_connect("localhost""root""""crawler_index");

 
$main_url="http://php.net/";
 
$str file_get_contents($main_url);
 
 
// Gets Webpage Title
 
if(strlen($str)>0)
 {
  
$str trim(preg_replace('/\s+/'' '$str)); // supports line breaks inside <title>
  
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
  
$title=$title[1];
 }
    
 
// Gets Webpage Description
 
$b =$main_url;
 @
$url parse_url$b );
 @
$tags get_meta_tags($url['scheme'].'://'.$url['host'] );
 
$description $tags['description'];
    
 
// Gets Webpage Internal Links
 
$doc = new DOMDocument
 @
$doc->loadHTML($str); 
 
 
$items $doc->getElementsByTagName('a'); 
 foreach(
$items as $value
 { 
  
$attrs $value->attributes
  
$sec_url[]=$attrs->getNamedItem('href')->nodeValue;
 }
 
$all_links=implode(",",$sec_url);
 
 
/*
 if (!$conn) 
{
    //Error Message to show user in technical/development mode to see errors.
    die("Database Error : " . mysqli_error($conn));
    
    //Error Message to show User in Layman's mode to see errors.
    die("Database error.");
    exit();
}
*/
//mysqli_query($conn,"insert into crawler_index values($main_url,$title,$description,$all_links)");

//Insert the user's inputs into Mysql database using php's sql injection prevention method "Prepared Statements".
            
$stmt mysqli_prepare($conn,"INSERT INTO crawler_index(url,title,description,internal_url) VALUES (?,?,?,?)");
            
mysqli_stmt_bind_param($stmt,'ssss',$main_url,$title,$description,$all_links);
            
mysqli_stmt_execute($stmt);
            
            
//Check if user's registration data was successfully submitted or not.
            
if (!$stmt)
            {
                echo 
"Sorry! Our system is currently experiencing a problem indexing your website.";
                exit();
            }
            
echo 
"Url: $main_url<br>";
echo 
"Title: $title<br>";
echo 
"Description: $description<br>";
echo 
"Links: $all_links<br>";

?>

The code I found here:
Create Simple Web Crawler Using PHP And MySQL
Then I updated it by adding PREP STMT.
My update should work. Why am I getting this totally & utterly nuisansical error about undefined index "description" when I do have a column under that name ?
Testing the script before attempting to learn from it's tutorial site as I do not want to learn buggy or outdated code.
Attached Images
]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/error-undefined-index-980654.html
Allowing users to add HTML blocks of code to their pages http://forums.devshed.com/php-development/980652-allowing-users-add-html-blocks-code-pages-new-post.html Mon, 21 May 2018 10:06:00 GMT Hi; Just making a simple page builder, need to allow users to add HTML blocks. That includes Js and all sort of things I gotta be careful with. Obviously raises flags. They paste it in the textare. Hi;

Just making a simple page builder, need to allow users to add HTML blocks.

That includes Js and all sort of things I gotta be careful with.

Obviously raises flags.

They paste it in the textare.

You know how it is.

How can I make sure it's secure?

At this stage all I know is when they add script before I preview I change the < and > to &gt; and &lt;

Thanks ]]>
PHP Development English Breakfast Tea http://forums.devshed.com/php-development-5/allowing-users-add-html-blocks-code-pages-980652.html
Accordion Sections without Submitting Forms http://forums.devshed.com/php-development/980646-accordion-sections-submitting-forms-new-post.html Sun, 20 May 2018 13:23:19 GMT Hi:

I am trying to create an "accordion" section on an input form to show and hide various portions of the page according to what the user needs to see.

I have been using the following as a guide:
https://www.w3schools.com/howto/tryi...w_js_accordion

The problem I am having is that the accordion buttons submit the form in order to refresh the display. However, at the time the user might want to show/hide sections, the form is not complete and submitting it prematurely causes problems.

Can anyone suggest a correction that will dynamically show/hide page sections without submitting the page?

Thanks ]]>
PHP Development SeanF http://forums.devshed.com/php-development-5/accordion-sections-submitting-forms-980646.html
Foreach Looping Levels - I got it right or wrong ? http://forums.devshed.com/php-development/980645-foreach-looping-levels-wrong-new-post.html Sun, 20 May 2018 11:29:37 GMT Php Buddies,

What I am trying to do is learn to build a simple web crawler.
So at first, I will feed it a url to start with.
It will then fetch that page and extract all the links into a single array.
Then it will fetch each of those links pages and extract all their links into a single array likewise. It will do this until it reaches it's max link deep level.
Here is how I coded it:

PHP Code:

<?php 

include('simple_html_dom.php'); 

$current_link_crawling_level 0
$link_crawling_level_max 2

if($current_link_crawling_level == $link_crawling_level_max)
{
    exit(); 
}
else
{
    
$url 'https://www.yahoo.com'
    
$curl curl_init($url); 
    
curl_setopt($curlCURLOPT_RETURNTRANSFER1); 
    
curl_setopt($curlCURLOPT_FOLLOWLOCATION1); 
    
curl_setopt($curlCURLOPT_SSL_VERIFYPEER0); 
    
curl_setopt($curlCURLOPT_SSL_VERIFYHOST0); 
    
$html curl_exec($curl); 
    
    
$current_link_crawling_level++;    

    
//to fetch all hyperlinks from the webpage 
    
$links = array(); 
    foreach(
$html->find('a') as $a
    { 
        
$links[] = $a->href
        echo 
"Value: $value<br />\n"
        
print_r($links); 
        
        
$url '$value'
        
$curl curl_init($value); 
        
curl_setopt($curlCURLOPT_RETURNTRANSFER1); 
        
curl_setopt($curlCURLOPT_FOLLOWLOCATION1); 
        
curl_setopt($curlCURLOPT_SSL_VERIFYPEER0); 
        
curl_setopt($curlCURLOPT_SSL_VERIFYHOST0); 
        
$html curl_exec($curl); 

        
//to fetch all hyperlinks from the webpage 
        
$links = array(); 
        foreach(
$html->find('a') as $a
        { 
            
$links[] = $a->href
            echo 
"Value: $value<br />\n";
            
print_r($links); 
            
$current_link_crawling_level++;
        } 
    echo 
"Value: $value<br />\n";
    
print_r($links);  
}

?>

I have a feeling I got confused and messed it up in the foreach loops. Nestled too much. Is that the case ? Hint where I went wrong.

Unable to test the script as I have to first sort out this error:
Fatal error: Uncaught Error: Call to a member function find() on string in C:\xampp\h

After that, I will be able to test it. Anyway, just looking at the script, you think I got it right or what ?

Thanks ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/foreach-looping-levels-wrong-980645.html
Fatal error: Uncaught Error: Call to a member function find() on string in C:\xampp\h http://forums.devshed.com/php-development/980644-fatal-error-uncaught-error-call-function-string-xampp-new-post.html Sun, 20 May 2018 10:10:33 GMT Php Folks, My following 3 codes work. The final two are the same except the latter one suppresses the WARNINGS. Now, the difference between the first one and the second one is that the first one uses file_get_html() function while the second one uses cURL (aswell as the third one). Anyway, trying to combine the two codes for some reasons which I will get into once this problem is solved.... Php Folks,

My following 3 codes work.
The final two are the same except the latter one suppresses the WARNINGS.
Now, the difference between the first one and the second one is that the first one uses file_get_html() function while the second one uses cURL (aswell as the third one).

Anyway, trying to combine the two codes for some reasons which I will get into once this problem is solved.
I get error:
Fatal error: Uncaught Error: Call to a member function find() on string in C:\xampp\htdocs\cURL\crawler.php:24 Stack trace: #0 {main} thrown in C:\xampp\htdocs\cURL\crawler.php on line 24


WORKING CODE 1
PHP Code:

FINDING HTML ELEMENTS BASED ON THEIR TAG NAMES

Suppose you wanted to find each 
and every image on a webpage or sayeach and every hyperlink
We will be using “find” function to extract this information from the objectHere’s how to do it using Simple HTML DOM Parser :
*/

include(
'simple_html_dom.php');

$html file_get_html('http://google.com');

//to fetch all hyperlinks from a webpage
$links = array();
foreach(
$html->find('a') as $a) {
 
$links[] = $a->href;
}
print_r($links);
echo 
"<br />";
 
//to fetch all images from a webpage
$images = array();
foreach(
$html->find('img') as $img) {
 
$images[] = $img->src;
}
print_r($images);
echo 
"<br />";
 
//to find h1 headers from a webpage
$headlines = array();
foreach(
$html->find('h1') as $header) {
 
$headlines[] = $header->plaintext;
}
print_r($headlines);
echo 
"<br />";

?> 


WORKING CODE 2
PHP Code:

<?php

/*
2a. Scrape Urls & Anchors And Echo Them By NOT Suppressing Warnings.

Using PHP's DOM functions to
fetch hyperlinks and their anchor text
*/

$url 'https://google.com';
$curl curl_init($url);
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
curl_setopt($curlCURLOPT_FOLLOWLOCATION1);
curl_setopt($curlCURLOPT_SSL_VERIFYPEER0);
curl_setopt($curlCURLOPT_SSL_VERIFYHOST0);
$data curl_exec($curl);

// DO NOT Hide HTML warnings
libxml_use_internal_errors(true);
$dom = new DOMDocument;
    
// echo Links and their anchor text
    
echo '<pre>';
    echo 
"Link\tAnchor\n";
    foreach(
$dom->getElementsByTagName('a') as $link) {
        
$href $link->getAttribute('href');
        
$anchor $link->nodeValue;
        echo 
$href,"\t",$anchor,"\n";
    }
    echo 
'</pre>';

?>


WORKING CODE 3
PHP Code:

<?php

/*
2b. Scrape Urls & Anchors And Echo Them By Suppressing Warnings.

Using PHP's DOM functions to
fetch hyperlinks and their anchor text
*/

$url 'https://google.com';
$curl curl_init($url);
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
curl_setopt($curlCURLOPT_FOLLOWLOCATION1);
curl_setopt($curlCURLOPT_SSL_VERIFYPEER0);
curl_setopt($curlCURLOPT_SSL_VERIFYHOST0);
$data curl_exec($curl);

// Hide HTML warnings
libxml_use_internal_errors(true);
$dom = new DOMDocument;
if(
$dom->loadHTML($dataLIBXML_NOWARNING)){
    
// echo Links and their anchor text
    
echo '<pre>';
    echo 
"Link\tAnchor\n";
    foreach(
$dom->getElementsByTagName('a') as $link) {
        
$href $link->getAttribute('href');
        
$anchor $link->nodeValue;
        echo 
$href,"\t",$anchor,"\n";
    }
    echo 
'</pre>';
}else{
    echo 
"Failed to load html.";

}
?>


Here is the code I am getting the error:
PHP Code:

<?php 

/* FROM dom_crawler_NOTES.php file.

2.
FINDING HTML ELEMENTS BASED ON THEIR TAG NAMES

Suppose you wanted to find each and every link on a webpage. 
We will be using “find” function to extract this information from the object. Here’s how to do it using Simple HTML DOM Parser :
*/

include('simple_html_dom.php');

$url 'https://www.yahoo.com';
$curl curl_init($url);
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
curl_setopt($curlCURLOPT_FOLLOWLOCATION1);
curl_setopt($curlCURLOPT_SSL_VERIFYPEER0);
curl_setopt($curlCURLOPT_SSL_VERIFYHOST0);
$html curl_exec($curl);

//to fetch all hyperlinks from a webpage
$links = array();
foreach(
$html->find('a') as $a) {
 
$links[] = $a->href;
}
print_r($links);
echo 
"<br />";

?>

Fatal error: Uncaught Error: Call to a member function find() on string in C:\xampp\htdocs\cURL\crawler.php:24 Stack trace: #0 {main} thrown in C:\xampp\htdocs\cURL\crawler.php on line 24


Why error in this line ? ....
PHP Code:

foreach($html->find('a') as $a) { 

Why script failing to acknowledge the "find" when it exists in the file:
include('simple_html_dom.php');

The simple_html_dom.php file can be downloaded from here:
https://sourceforge.net/projects/simplehtmldom/files/

Look at the first 3 scripts in this post. The first script uses this "simple_html_dom.php" and it had no problem using the "find".
Since on my latter script's code I referenced it to this "simple_html_dom.php" and it is residing in the same directory on my xamp then I should not be getting this silly error:
Fatal error: Uncaught Error: Call to a member function find() on string in C:\xampp\htdocs\cURL\crawler.php:24 Stack trace: #0 {main} thrown in C:\xampp\htdocs\cURL\crawler.php on line 24


Now should I ? :confused: ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/fatal-error-uncaught-error-call-function-string-xampp-980644.html
DOM Parser Stuffs http://forums.devshed.com/php-development/980637-dom-parser-stuffs-new-post.html Thu, 17 May 2018 16:30:24 GMT Php Folks,

If you find or know of any DOM Parser tutorial links that will be good for newbies then let us all know here.
When you paste the links, mention whether it's for php beginners, intermediates or advanced programmers.
Anyway, I just came across this one and reading it.
Just thought I might aswell open a thread and mention the link just incase it becomes handy for others.

Top 10 Best Usage Examples of PHP Simple HTML DOM Parser ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/dom-parser-stuffs-980637.html
Validate date and time. Taking science to the next level http://forums.devshed.com/php-development/980633-validate-date-time-taking-science-level-new-post.html Thu, 17 May 2018 03:24:37 GMT Hi; Does it have to be this long? Or can it be done with a magic wand? PHP: --------- public function is_valid_date($date=null) { if($date==null) Hi;

Does it have to be this long? Or can it be done with a magic wand?

PHP Code:

public function is_valid_date($date=null)
  {
    if(
$date==null)
      {
        return 
false;
      }
    
$d explode("-"$date);  
    if(
is_array($d) && count($d)==3)
      {
        if(!
checkdate($d[1],$d[2],$d[0]))
          {
            return 
false;
          }
        else 
          {
            return 
true;
          }
      }    
    else 
      {
        return 
false;
      }    
  }
public function 
is_valid_time($time=null)
  {
    if(
$time==null)
      {
        return 
false;
      }
    if(
strtotime($time))
      {
        return 
true;
      }          
    else 
      {
        return 
false;
      }  
  } 

PHP Code:

public function is_plc_available_from_valid($plc_id)
{
   
$CI =& get_instance();
   if(
$CI->launch_library->launch_type_by_plc_id($plc_id)=='evergreen')
    {
      return 
true;
    }
   if(!isset(
$_POST['broadcast_access_date']) || !isset($_POST['broadcast_access_time']) )
      {
        return 
5;
      }
     if(!
$CI->custom_functions->is_valid_date($_POST['broadcast_access_date']))
      {
        return 
4;
      }
    if(!
$CI->custom_functions->is_valid_time($_POST['broadcast_access_time']))
      {
        return 
3;
      }  
    
    if(
$CI->plc_library->launch_offer_open_date_time_by_plc_id($plc_id) < strtotime($_POST['broadcast_access_date']." ".$_POST['broadcast_access_time']))
      {
        return 
2;
      }
    else 
      {
        return 
1;
      }  


]]>
PHP Development English Breakfast Tea http://forums.devshed.com/php-development-5/validate-date-time-taking-science-level-980633.html
Is there a simple way to find all places printing user entered data? http://forums.devshed.com/php-development/980632-simple-printing-user-entered-data-new-post.html Thu, 17 May 2018 02:02:01 GMT Hi hello;

I wanna clean some code but it's a bit big.

Is there a simple way to find all places the app prints user entered data to make sure html_escpae is applied?

Thanks ]]>
PHP Development English Breakfast Tea http://forums.devshed.com/php-development-5/simple-printing-user-entered-data-980632.html
How To Extract Urls And Their Anchors From Links On Any Page Desired ? http://forums.devshed.com/php-development/980630-extract-urls-anchors-links-page-desired-new-post.html Wed, 16 May 2018 23:53:06 GMT Php Buds,

Here's the code, using DOM for grabbing links from google:

PHP Code:

    <?php

    
# Use the Curl extension to query Google and get back a page of results
    
$url "http://www.google.com";
    
$ch curl_init();
    
$timeout 5;
    
curl_setopt($chCURLOPT_URL$url);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
    
$html curl_exec($ch);
    
curl_close($ch);

    
# Create a DOM parser object
    
$dom = new DOMDocument();

    
# Parse the HTML from Google.
    # The @ before the method call suppresses any warnings that
    # loadHTML might throw because of invalid HTML in the page.
    
@$dom->loadHTML($html);

    
# Iterate over all the <a> tags
    
foreach($dom->getElementsByTagName('a') as $link) {
            
# Show the <a href>
            
echo $link->getAttribute('href');
            echo 
"<br />";
    
    
?>

It echoes results sort of like this:

https://www.google.com.com/webhp?tab=ww
http://www.google.com.com/imghp?hl=bn&tab=wi
http://maps.google.com.com/maps?hl=bn&tab=wl


Now, I'd like to convert the above code so again using DOM it is able to extract all urls and their anchor texts from all links residing on any chosen webpage no matter what format the links are in.
Formats such as:

<a href="http://example1.com">Test 1</a>
<a class="foo" id="bar" href="http://example2.com">Test 2</a>
<a onclick="foo();" id="bar" href="http://example3.com">Test 3</a>

The anchor texts should sit underneath each extracted url. And there should be a line in-between each listed item. Such as:


http://stackoverflow.com<br>
A programmer's forum<br>
<br>
http://google.com<br>
A searchengine<br>
<br>
http://yahoo.com<br>
An Index<br>
<br>

And so on.
I'd also appreciate another version. This time a cURL version too (not using DOM). Which performs the same result.
This cURL (not using DOM) did not exactly work as was intended:

PHP Code:


    <?php

    
/*
    $curl = curl_init('http://devshed.com/');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

    $page = curl_exec($curl);

    if(curl_errno($curl)) // check for execution errors
    {
        echo 'Scraper error: ' . curl_error($curl);
        exit;
    }

    curl_close($curl);

    $regex = '<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]';
    if ( preg_match($regex, $page, $list) )
        echo $list[0];
    else 
        print "Not found"; 

    ?>

Any chance I can this achieved with cURL (not using DOM) without the regex ? You know I dislike regex and prefer simplicity in coding.
Nevertheless, from your end, I'd like to see a regex sample and another sample without regex. ;)
Oh by the way, I really prefer not to use limited functions such as the get_file() and the like. You know what I mean.

Give us your best shots!

Take care!

Cheers! ]]>
PHP Development UniqueIdeaMan http://forums.devshed.com/php-development-5/extract-urls-anchors-links-page-desired-980630.html