I connect up to a domain API and perform an availability check on only one domain.
I would like to loop this 10 times, in the most efficient way to check for any changes in status
I would like it to do the checks as quickly as possible (reduce the time between checks)
I would like it to ouput each time it completes a check (if a multiple loop is in place it only outputs all checks once finished, in one go, rather than one at a time after each check / iteration in the loop)
Cheers!
<?php
// connection credentials and settings
$location = 'https://TheApiServiceURL.com/';
$wsdl = $location.'?wsdl';
$username = 'APIuser';
$password = 'APIpass';
// include the console and client classes
include "class_console.php";
include "class_client.php";
// create a client resource / connection
$client = new Client($location, $wsdl, $username, $password);
/**
* Example usage and output results to screen
*/
// Example #1: Check domain name availability
print('========== consoleMethod[domainLookup] ==========<br/>');
$client-‐>set('domain', 'domain.com');
$client-‐>domainLookup();
$client-‐>screen($client-‐>response());
$client-‐>unset('domain');
?>
I googled sub-strings of your code. I found the documentation - the provided code is from the examples section.
About the Class "Client"
This is what it says about the screen method:
public function screen($var)
{
print '<pre>';
print_r($var);
print '</pre>';
return $this‐>connection;
}
and
public function response()
{
return $this->response;
}
Efficiently Looping 10 Times
If you want to get your response every iteration (is what you want, right?), do this:
$client‐>set('domain', 'domain.com');
$i=0;
while($i<10)
{
$client‐>domainLookup();
echo $client‐>response();
// or $client‐>screen($client‐>response());
$i++;
}
$client->unset('domain');
According to this benchmark while beats for. But it will be a minor difference at 10 iterations. However, If you really do want to tweak it I suggest timing different approaches- maybe even trying to copy paste the commands 10 times.
DomainLookup() Speed
Or refered to as "Checking Speed" by you.
This depends on the function domainLookup() which is provided by the api. So you'll have to see what this function is doing if you want to make the "checking speed" quicker. You could multi-thread the function but php isn't really made for that.
Related
I am sorry to sound confusing but I will try to explain in the best way possible.
In the controller I have a function search
public function search(){
/*
I run my logics and get few URL from
where I need to fetch further data
the urls are saved in the URL array
$urls[0] = "http://url1.com/search1";
$urls[1] = "http://url2.com/search2";
I then set this in data variable and send it to view
so that It can be run in AJAX
I tired running get_file_contents but it executes
in series one after the other URL.
If there are 10 URL (5 secs per URL) the over all processing time
increases drastically
*/
$data["urls"] = $urls;
$resp = $this->load->view('ajaxer',$data,TRUE);
/* based on the $resp i need to run further business logic's */
}
Now the $resp is actually giving me only the HTML code. It is not executing the HTML and hence the ajax is not run.
Any thoughts on how to execute this will be really helpful.
Regards,
Amit
Your code is absolutelly ok. But your javascript is not getting any response data (only headers), because you are not returning any output.
If you want to "execute your HTML" you need to change the line with view to this:
$this->load->view('ajaxer',$data);
or this:
$resp = $this->load->view('ajaxer',$data,TRUE);
echo $resp;
You forgot to echo output in the controller. Apart from this you need few minor modification in your function.
public function search(){
/*
I run my logics and get few URL from
where I need to fetch further data
the urls are saved in the URL array
$urls[0] = "http://url1.com/search1";
$urls[1] = "http://url2.com/search2";
I then set this in data variable and send it to view
so that It can be run in AJAX
I tired running get_file_contents but it executes
in series one after the other URL.
If there are 10 URL (5 secs per URL) the over all processing time
increases drastically
*/
// You need to check either request came from Ajax request or not. If not it will echo passed string. It prevents to access this function besides Ajax request
if (!$this->input->is_ajax_request()) {
echo "Ajax Requests allowed.";
die;
}
$data["urls"] = $urls;
$resp = $this->load->view('ajaxer',$data,TRUE);
// Standard way to set response for output in json format.
// #param status will help to check all things goes correct or not. if not please pass false on the basis or your feature's requirement
$this->output->set_output(json_encode(array('status'=>true,'response'=>$resp)));
// Standard way to get output set above step.
$string = $this->output->get_output();
echo $string;
exit();
/* based on the $resp i need to run further business logic's */
}
Updated code is here. Hope you find your answer
I'm developing a tool for a website and I came up with an odd problem, or better, an odd situation.
I'm using the code bellow to retrieve data from the TeamSpeak server. I use this info to build a profile on a user.
$ts3 = TeamSpeak3::factory("serverquery://dadada:dadada#dadada:1234/");
// Get the clients list
$a=$ts3->clientList();
// Get the groups list
$b=$ts3->ServerGroupList();
// Get the channels list
$c=$ts3->channelList();
Now, the odd situation is that the output of this code block:
// Get the clients list
$a=$ts3->clientList();
// Get the groups list
$b=$ts3->ServerGroupList();
// Get the channels list
$c=$ts3->channelList();
echo "<pre>";print_r($a);die();
(Notice the print_r)
Is totally different from the output of this code block:
// Get the clients list
$a=$ts3->clientList();
// Get the groups list
#$b=$ts3->ServerGroupList();
// Get the channels list
#$c=$ts3->channelList();
echo "<pre>";print_r($a);die();
What I mean is, the functions I call after clientList() (which output I store in the variable $a) are changing that variable's contents. This is, they're kind of appending their output to the variable.
I've never learned PHP professionally, I'm just trying it out... Am I missing something about this language that justifies this behavior? If I am, what can I do to stop it?
Thank you all.
You're seeing parts of the "Object" in Object Oriented Programming
$ts3 represents an Object containing all the information needed, along with some methods (or functions) that let you get data from the object. Some of these methods will do different things to the object itself, in order to retrieve additional data needed for a particular method call.
Consider the following simple Object:
Bike
color
gears
function __construct($color, $gears)
this.color = $color; this.gears = $gears
function upgrade()
this.headlight = true; this.gears = 10;
Now, when you first create it, it only has two properties:
$myBike = new Bike('red',5);
// $myBike.color = 'red';
// $myBike.gears = 5;
...but once you upgrade, properties have changed, and new ones are added.
$myBike->upgrade();
// $myBike.color = 'red';
// $myBike.gears = 10;
// $myBike.headlight = true;
Objects usually pass references rather than copying data, in order to save memory.
...but if you want to make sure that you're getting a copy that won't change (i.e. does not use data references to the $ts3 object), clone the variable.
$a = clone($ts3->clientList());
Be warned, this will effectively double the memory and processor usage for that variable.
There is useful method getStats in Db-component Yii
$sql_stats = YII::app()->db->getStats();
echo $sql_stats[0] //the number of SQL statements executed
echo $sql_stats[1] //total time spent
Official documentation link
Is there method in Yii2 to get this information?
Here is equivalent for Yii 2:
$profiling = Yii::getLogger()->getDbProfiling();
$profiling[0] contains total count of DB queries, $profiling[1] - total execution time.
Note that if you want to get information about all queries at the end of request you should execute this code in right place, for example in afterAction():
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
$profiling = Yii::getLogger()->getDbProfiling();
...
return $result;
}
Otherwise you will get the information according to the moment of execution this command.
Official documentation:
getDbProfiling()
afterAction()
If you enable the debugging toolbar you get a lot more info, it is much much better than this. Also you can enable logging that should also get you much more info.
More info on the debugging toolbar here http://www.yiiframework.com/doc-2.0/ext-debug-index.html and more info about the logging here http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
I'm not sure if this is possible but thought I would ask anyway.
I am developing a wordpress based website that already has quite a bit of data in the database.
As I write new code for the site I use a function I've written (getDebug) to 'email' myself what is happening in the code.
A few times now (due to either carelessness or ignorance) I've left my debug function in recursive loops that loop through every 'post' in the database. The result is that I lock up my test site whilst it happily sends me 10,000 emails.
I wondered if it is possible to detect how many times a function has been called within a certain time-span and if it exceeds a number then stop performing the action.
I wondered if this could be done with sessions?
Here's an example:
foreach($allPostIDs as $postID) {
getDebug("Check postID ".$postID); // oops, I'm going to email on each iteration
// rest of foreach....
}
I know the following won't work but this gives you an idea of what I would like to do (unless there is a better way of achieving this):
/* ======================================================= */
// SENDS AN EMAIL TO ADMIN WITH DEBUG INFO
/* ======================================================= */
if (!function_exists('getDebug')) {
function getDebug($var = null, $extraInfo = null) {
if(
$_SESSION("HowManyTimesCalled") < 10 &&
$_SESSION("TimeSinceLastCall") > 10
) {
$adminEmail = new FAR_Email_Class();
$adminEmail->addDebugVar($var);
$adminEmail->addExtraInfo($extraInfo);
$adminEmail->sendEmail();
}
$_SESSION("HowManyTimesCalled")++;
$_SESSION("TimeSinceLastCall") = time();
}
}
/* ======================================================= */
// END OF FUNCTION
/* ======================================================= */
I'm not sure if the $_SESSION vars would be updated though if the browser isn't refreshed?
Any ideas on how to achieve this?
If you're wanting to detect how many times for a single user, $_SESSION could work.
If you want to detect how many times across all users (sessions), you'll need to store it in the db or on the filesystem or in a memory cache like redis.
Greetings,
I already have a working connection to the AD and can search and retrieve information from it. I've even developed a recursive method by which one can retrieve all groups for a given user. However, I'd like to avoid the recursion if possible. One way to do this is to get the tokenGroups attribute from the AD for the user, which should be a list of the SIDs for the groups that the specified user has membership, whether that membership be direct or indirect.
When I run a search for a user's AD information, though, the tokenGroups attribute isn't even in it. I tried specifically requesting that information (i.e., specifying it using the fourth parameter to ldap_search) but that didn't work, either.
Thanks,
David Kees
Solved my own problem and thought I'd put the answer here so that others might find it. The issue was using the ldap_search() function. The answer was to use the ldap_read() function instead of ldap_search(). The difference is the scope of the request. The search function uses a scope of "sub" (i.e., subtree) while the read function uses "base." The tokenGroups information can only be found when using a scope of "base" so using the correct PHP function was the key.
As I mentioned above, I was working from someone else code in perl to create my solution and the perl script used a function named "search" to do it's LDAP requests which lead me down wrong path.
Thanks to those who took a peek at the question!
--
As per the requests in the comments, here's the basics of the solution in code. I'm extracting from an object that I use so this might not be 100% but it'll be close. Also, variables not declared in this snipped (e.g. $server, $user, $password) are for you to figure out; I won't know your AD credentials anyway!
$ldap = ldap_connect($server);
ldap_bind($ldap, $user, $password);
$tokengroups = ldap_read($ldap, $dn, "CN=*", array("tokengroups")));
$tokengroups = ldap_get_entries($ldap, $tokengroups);
At this point, $tokengroups is our results as an array. it should have count index as well as some other information. To extract the actual groups, you'll need to do something like this:
$groups = array();
if($tokengroups["count"] > 0) {
$groups = $tokengroups[0]["tokengroups"];
unset($groups["count"]);
// if you want the SID's for your groups, you can stop here.
// if you want to decode the SID's then you can do something like this.
// the sid_decode() here: http://www.php.net/manual/en/function.unpack.php#72591
foreach($groups as $i => &$sid) {
$sid = sid_decode($sid);
$sid_dn = ldap_read($ldap, "<SID=$sid>", "CN=*", array("dn"));
if($sid_dn !== false) {
$group = ldap_get_entries($ldap, $sid_dn);
$group = $group["count"] == 1 ? $group[0]["dn"] : NULL;
$groups[$i] = $group;
}
}
}
That's the basics. There's one caveat: you'll probably need to work with the individual or individuals who manage AD accounts at your organization. The first time I tried to get this running (a few years ago, so my memory is somewhat fuzzy) the account that I was given did not have the appropriate authorization to access the token groups information. I'm sure there are other ways to do this, but because I was porting someone else's code for this specific solution, this was how I did it.