Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-04-26 23:26:46
Point:500 Replies:5 POST_ID:828488USER_ID:11059
Topic:
PHP Scripting Language;;
I am using LoadHTMLFile("http://www.otherwist.com/") to load the html file from remote site's root in PHP
and I found sometimes it can be hang up the program because the link at remote site is not ready yet.
But how I can set timer in php if the waiting for LoadHTMlFile() function is more than 10 seconds, and then re-do it again ? That is my first question.
THe second question in other case, if php run it and quit the program since it has fatal-error, how can restart it
the php program again automatically ? Is the function existing in PHP like on error resume next or goto A1: in VBA ?
Please advise
Duncan
and I found sometimes it can be hang up the program because the link at remote site is not ready yet.
But how I can set timer in php if the waiting for LoadHTMlFile() function is more than 10 seconds, and then re-do it again ? That is my first question.
THe second question in other case, if php run it and quit the program since it has fatal-error, how can restart it
the php program again automatically ? Is the function existing in PHP like on error resume next or goto A1: in VBA ?
Please advise
Duncan
Author: duncanb7 replied at 2024-04-27 22:21:23
Thanks for your reply
it is solved by time-out at
this EE thread:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26980898.html
once time-out do something in
code if need
it is solved by time-out at
this EE thread:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26980898.html
once time-out do something in
code if need
Accepted Solution
Expert: Ray Paseur replied at 2024-04-27 06:52:04
350 points EXCELLENT
I think you might want to do this process in two steps. Rather than trying to load a remote file directly, you might want to use CURL to read the file into a variable string and then process the string. When you read remote files with things like file_get_contents() and the like you run the risk that the remote service will be slow or dead. If that happens, your script hangs and eventually gets killed for too much execution time. CURL gives you a way around that problem because it will return an error indicator of the timeout and your scripts can handle the timeout error condition.
Example of a GET-method CURL script in the code snippet. You can see it in action here:
http://www.laprbass.com/RAY_curl_example.php
But a word of caution is in order. Some sites do not allow you to access them in an automated way. Be certain that you have permission from the site owners before you do this. Usually if they want to allow automated access to their data, they will expose an API. Then you will not have to write all the scraping code, and more importantly you will be able to use their data in compliance with their terms of service.
Best of luck with the project, ~Ray
Example of a GET-method CURL script in the code snippet. You can see it in action here:
http://www.laprbass.com/RAY_curl_example.php
But a word of caution is in order. Some sites do not allow you to access them in an automated way. Be certain that you have permission from the site owners before you do this. Usually if they want to allow automated access to their data, they will expose an API. Then you will not have to write all the scraping code, and more importantly you will be able to use their data in compliance with their terms of service.
Best of luck with the project, ~Ray
<?php // RAY_curl_example.phperror_reporting(E_ALL);// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVERfunction my_curl( $url, $get_array=array(), $timeout=3, $error_report=TRUE){ // PREPARE THE ARGUMENT STRING IF NEEDED $get_string = ''; foreach ($get_array as $key => $val) { $get_string = $get_string . urlencode($key) . '=' . urlencode($val) . '&'; } $get_string = rtrim($get_string, '&'); if (!empty($get_string)) $url .= '?' . $get_string; $curl = curl_init(); // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header[] = "Cache-Control: max-age=0"; $header[] = "Connection: keep-alive"; $header[] = "Keep-Alive: 300"; $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php curl_setopt( $curl, CURLOPT_URL, $url ); curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' ); curl_setopt( $curl, CURLOPT_HTTPHEADER, $header ); curl_setopt( $curl, CURLOPT_REFERER, 'http://www.google.com' ); curl_setopt( $curl, CURLOPT_ENCODING, 'gzip,deflate' ); curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE ); curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt( $curl, CURLOPT_TIMEOUT, $timeout ); // RUN THE CURL REQUEST AND GET THE RESULTS $htm = curl_exec($curl); // ON FAILURE HANDLE ERROR MESSAGE if ($htm === FALSE) { if ($error_report) { $err = curl_errno($curl); $inf = curl_getinfo($curl); echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err"; var_dump($inf); } curl_close($curl); return FALSE; } // ON SUCCESS RETURN XML / HTML STRING curl_close($curl); return $htm;}// USAGE EXAMPLE - PUT YOUR FAVORITE URL HERE$url = "http://finance.yahoo.com/d/quotes.csv";// PUT YOUR ARRAY OF KEY=>VALUE PAIRS HERE$arg = array( 's' => 'lulu', 'f' => 'snl1c1ohgvt1');// MAKE THE CALL$htm = my_curl($url, $arg, 2, TRUE);if (!$htm) die("NO $url");// SHOW WHAT WE GOTecho "<pre>";var_dump($arg);echo PHP_EOL . $url;echo PHP_EOL . htmlentities($htm);echo PHP_EOL;// TRY ANOTHER URL WITHOUT ARGUMENTS$url = 'http://twitter.com';$htm = my_curl($url);echo PHP_EOL . $url;echo PHP_EOL . htmlentities($htm);echo PHP_EOL; 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:83:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:100:101:102:103:104:
Author: duncanb7 replied at 2024-04-27 01:01:40
Just extract 1 or 2 data from one home page at http://www.othersite.com/
Is it a way to do for safe ? First using
$file_url=file_get_content(url);
$dom->loadHTML($file_url);
Those two code replay $dom->loadHTMlFile(url) will be less error compliant during fire webpage, Is it true ?
Please advise, I also use @ sign to suppress error but it is not good way to avoid the error,
What I need, if it is error go to re-do
Is it a way to do for safe ? First using
$file_url=file_get_content(url);
$dom->loadHTML($file_url);
Those two code replay $dom->loadHTMlFile(url) will be less error compliant during fire webpage, Is it true ?
Please advise, I also use @ sign to suppress error but it is not good way to avoid the error,
What I need, if it is error go to re-do
Assisted Solution
Expert: AnuarJamlus replied at 2024-04-27 00:15:48
50 points EXCELLENT
Are you trying to load a html file to save in an xml document?
Assisted Solution
Expert: vimalmaria replied at 2024-04-27 00:14:05
100 points EXCELLENT
Hai,
You may go throw the following,can get ideas,
http://php.net/manual/en/domdocument.loadhtmlfile.php
http://php.net/manual/en/domdocument.loadhtml.php
You may go throw the following,can get ideas,
http://php.net/manual/en/domdocument.loadhtmlfile.php
http://php.net/manual/en/domdocument.loadhtml.php