I'm trying to create a php script which retries another php script up to 3 times until an error is displayed. I'm thinking perhaps this could be done using a php loop? If the code works successfully the first time, then there is no need for it to retry 3 times, however, if it doesn't work the first time, then it should retry the php script up to 3 times till an error message is displayed.
Using php coding, I've managed to make a script which grabs/fetches content from another location using "file_get_contents" and thereafter gives each word/data a php variable. All this was done by getting help from other members on stackoverflow (which I extremely appreciate). The code below is what does it all:
$searchdata = file_get_contents('http://www.example.com');
list($no1, $no2, $no3, $no4, $no5,
$no6, $no7, $no8, $no9) = explode(" ", $searchdata);
So, I'd like to add some sort of loop which retries this script up to 3 times; if it doesn't work the first time. To determine whether the script works the first/second/third time or not, the text "#endofscript" or "failure" should be found when using "file_get_contents". If anything else is displayed other than "#endofscript" or "failure" that should be counted as an error and should be looped till found. If it still isn't found after the third try, could an error message be displayed? Such as "Error - Please try again".
Thank you for all your assistance and I will appreciate each and every single reply. If you need more details, please feel free to ask. And again, I'm really grateful for this. :)
$maxTries = 3;
for ($try=1; $try<=$maxTries; $try++) {
// your code
if ($success) {
break;
}
}
// if $try > 3, script failed
Related
This is a first time we are using GSS and applying in our application.
If we search a query in GSS we are getting good result depending upon the websites we have added to be searched from. But if we give GSS around a hundred queries one by one in a for loop, like,
for ($i = 0, $count = count($arr1); $i < $count; $i++)
{
print $arr1[$i]."\r\n\r\n";
sleep(5);
$in = $arr1[$i];
$in = str_replace(' ','+',$in); // space is a +
//google site search start here
$result = httpGet("https://www.google.com/cse?cx=003255331468891731323:xyxyxyxyxyyx&client=google-csbe&output=xml_no_dtd&q='$in'");
echo $result;
}
Here we have a long string of few pages which we have broken it into small arrays of say 30 words each. These array we have passed in a FOR loop, to get the result (various links) , we have printed the result with echo. We have also applied Sleep of 5 sec so that server gets time to get the result and print it , wait for 5 seconds before searching another query.
But when we are running this for loop, we are not getting result , rather our application hangs, and gives us the result as below:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#checkforplag.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
error screenshot
Kindly suggest what all do we need to do to apply GSS completely in our application.
Thank You!
I got solution for that by sending my php script in background process, and for that i used shell_exec.
This is the code i used:
$status = shell_exec("nohup /usr/bin/php test.php > /dev/null 2>&1 &");
And now i am not getting this kind of error even i am running a large file.
I have built a query ($query_q = "SELECT * FROM `table`;") and am trying to execute it within a function.
public function read_from_table() {
$query_q = "SELECT * FROM `table`";
$query_a = mysql_query($query_q);
while (mysql_fetch_array($query_a)) {
echo "Did it!";
//OR AS TRIED ANOTHER WAY
return "Did it!";
}
}
And called as such:
echo $classInstance->read_from_table();
//OR AS TRIED ANOTHER WAY
$classInstance->read_from_table();
Both the ways that the function and the class have been made have been tried in every conceivable way, and yet I still get no result.
I was getting the error that says the xecution time has exceeded the limit of 30 seconds, so I added the ini_set('max_execution_time', 0); (knowing this removes time limit altogether) to see if the query would execute at all, it has been running now for 30 minutes without a sign of life. Why is the query not executing?
Additional comments:
I am aware that I am using the depreciated mysql_* functions, this is at client request and will be updated after the site has been made live and is complete to a point where I am ready to change it all to mysqli->* functions.
The table that I am calling (it's name has been stripped and replaced with `table`) has only 9 rows in it, so this should not affect the execution time greatly (or will it?).
I have had to strip all sensitive information from the function to satisfy the client and my employer. Please keep in mind that I cannot disclose and information that the client and my employer do not wish to disclose.
The issue was that the internet and server had gone down.
This has since been sorted and is operational.
Thank you for help and support in this.
DigitalMediaMan
try
error_reporting(E_ALL);
if all ok, try run this query from console, look, how many times query will be performed
before this, kill old process in database(show processlist and kill pid)
I coded a function to help me handle transaction with files in CodeIgniter.
today I was trying this code:
function($db_trans_func, $context){
if(is_callable($db_trans_func)){
$context = $db_trans_func($context);
FirePHP_::info_(time(), "After Db trans");
}
}
that is just a snippet from my helper. But the problem is, when this code runs and in the case where the execution of the function $db_trans_func takes place it takes more time to run, php passes to next code FirePHP_::info_($context, "From db transaction"); before the ending of the line before.
That is abnormal for me. because in the normal case the lines should run one after the other.
Can anyone help me solve this problem ? How can I tell php to not run
FirePHP_::info_(time(), "After Db trans");
after that:
$context = $db_trans_func($context);
finishes its execution?
I'm not entirely clear, but my assumption is:
db_trans_func is running some function against the DB (such as setting a transaction begin)
you are comparing the php function FirePHP_::info_(time(), "After Db trans"); against the time recorded in the db, or similar
In other words, you have a function that DOES fire first in php, then a second one. They ARE running consecutively; BUT, the DB result takes longer, of course, and so the db effect is seen afterwards. In other words, these are different threads running asynchronously
Does that make sense to you, and is it possible?
I have a block of code and want to run it after certain time intervals, say after every 3 seconds. If I get required result it should die otherwise another request will be sent after 3 seconds. Any idea to do it with php.
Thanks in advance for any help
You can sleep in a loop:
while (true) {
$result = doSomething;
if (resultIsGood) {
break;
}
sleep(3);
}
If you don't want to keep the browser waiting, you can look up ignore_user_abort() solutions on Google.
If what you want to execute MySQL queries (and nothing else), maybe using the MySQL event scheduler could fit your need:
http://dev.mysql.md/doc/refman/5.5/en/events-overview.html
I wrote a script downloading a list of pages from a website. From time to time I receive the following error (the number of seconds is variable):
The bwshare module will refuse your requests for the next 7 seconds.
You have downloaded data too rapidly.
I found when using sleep(2) in the loop, it works much better, however the time delay is too expensive.
What's the best way how to deal with this module? Should I scrape it without any delay and if the response will be similar to the above message simply use sleep for the requested number of seconds?
It all depends on how many pages you can get before the error message.
Try and measure how many pages in average you can get.
4 pages before the bwshare message is the minimum.
If you are getting the error message before reaching 4 page downloads, then il would be faster to sleep(2) after each download.
try this way... it might help u.
$requestTime = 0.1; // s/connection
foreach(/* blah */) {
$start = microtime(true);
// Do your stuff to here.. get_file_content($url) and other processing .........
if($timeTaken = microtime(true)-$start < $requestTime) {
usleep(($requestTime-$timeTaken)*1000000);
}
}
if your problem is solved then try to post your answer so that other people may also be benefited