Jump to content

JustinK101

Members
  • Posts

    503
  • Joined

  • Last visited

About JustinK101

  • Birthday 06/28/1982

Contact Methods

  • AIM
    JustinK101

Profile Information

  • Gender
    Male
  • Location
    San Diego, California, US

JustinK101's Achievements

Advanced Member

Advanced Member (4/5)

0

Reputation

  1. Found the issue. So the problem was that it does not like the custom header: Content-Type: application/json. When removed, it works. Any ideas why?
  2. I am calling CURL and trying to do a POST request with parameters: $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Accept: application/json", "Content-Type: application/json")); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_USERAGENT, "curl 7.23.1 (x86_64-unknown-linux-gnu)"); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_URL, "https://www.mydomain.com/route"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "key1=blah1&key2=blah2"); return curl_exec(curl); The problem, inside the request at http://www.mydomain.com/route I am not seeing any POST parameters passed. I.E. print_r($_POST); Array ( ) Should have key1=blah1 and key2=blah2. Any ideas?
  3. I am trying to reference the first element in the $_GET array, but it is being a pest, and saying Undefined offset: 0. Basically, I don't know the key, its dynamic. So: http://www.mydomain.com/mypage.php?dynamickey=somevalue I should be able to reference $_GET[0] correct?
  4. Here is CURL wrapper class we wrote. Enjoy. <?php /** * Curl * * @todo * * @version 1.0.2 * @date last modified 05/20/2011 * @author XXXX <http://www.XXXX.com> <hello@XXXX.com> * @copyright (c) 2011 XXXX. All Rights Reserved. */ class Curl { private $curl_object; public function __construct($p_username = "", $p_password = "", $p_timeout = 10) { $this->curl_object = curl_init(); curl_setopt($this->curl_object, CURLOPT_HTTPHEADER, Array("Accept: application/json", "Content-Type: application/json")); curl_setopt($this->curl_object, CURLOPT_CONNECTTIMEOUT, $p_timeout); curl_setopt($this->curl_object, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl_object, CURLOPT_SSL_VERIFYPEER, false); if(!empty($p_username) && !empty($p_password)) { curl_setopt($this->curl_object, CURLOPT_USERPWD, $p_username . ":" . $p_password); curl_setopt($this->curl_object, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } } public function get_request($p_url) { curl_setopt($this->curl_object, CURLOPT_URL, $p_url); return curl_exec($this->curl_object); } public function post_request($p_url, $p_post_data) { curl_setopt($this->curl_object, CURLOPT_URL, $p_url); curl_setopt($this->curl_object, CURLOPT_POST, true); curl_setopt($this->curl_object, CURLOPT_POSTFIELDS, $p_post_data); return curl_exec($this->curl_object); } public function close() { curl_close($this->curl_object); } } ?>
  5. @gizmola Here is the function you wanted to see by the way. No wizardry, just fast. public static function generate_machine_id($length = 15) { $password = ""; $possible = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $maxlength = strlen($possible); if ($length > $maxlength) { $length = $maxlength; } $i = 0; while ($i < $length) { $password .= substr($possible, mt_rand(0, $maxlength-1), 1); $i++; } return $password; }
  6. Stackoverflow to the rescue, here is the solution, runs in 2 seconds. for($i = 0; $i < 100000; $i++) { //Generate machine id returns a 15 character alphanumeric string $mid = Functions::generate_machine_id(); if (isset($machine_ids[$mid])) { die("Collision!"); } else { $machine_ids[$mid] = true; } }
  7. The generator algorithm inst the problem, ran the code without the check and under a second. I know I am checking 100,000. That's the entire point. Honestly, 100,000 element array inst that much in today's scale.
  8. I have the following simple code to test against collision on a primary key I am creating: $machine_ids = array(); for($i = 0; $i < 100000; $i++) { //Generate machine id returns a 15 character alphanumeric string $mid = Functions::generate_machine_id(); if(in_array($mid, $machine_ids)) { die("Collision!"); } else { $machine_ids[] = $mid; } } die("Success!"); Any idea why this is taking minutes to run? Anyway to speed it up?
  9. We have a complex, json object which is multiple levels deep. We need to search for a given property/key in the entire object (not just first occurrence, but all), and if we find the property, return where in the object each property exists, so we can modify the properties value. Figure it needs to be recursive.
  10. The only thing, is that I don't want Test to be an instance. It is static. So: class Test extends MyBase { public static function get_user_id() { echo $this->user_id; } } Obviously, if its static, can't use $this. Is there a way around this?
  11. Gizmola, First, who is calling the base constructor? I actually don't want to access user_id statically, but I think to use the keyword `parent` you have to use it like: parent::$user_id. Is there another way to access the property of the MyBase class $user_id from inside the Test class?
  12. If I have a base class MyBase: class MyBase { public $user_id = NULL; public function __construct() { $this->user_id = "justin"; } } Then I have a class which inherits the base class MyClass: class Test extends MyBase { public static function get_user_id() { echo parent::$user_id; } } Finally calling it: echo Test::get_user_id(); First question, do I have to create an instance of MyBase, or when Test extends MyBase will it instanciate it automatically and call the MyBase constructor? Second, I am getting the error: PHP Fatal error: Access to undeclared static property: MyBase::$user_id
  13. Thanks for the reply, but need a scalable solution, and a cron job, just doesn't feel right. We Googled around and found Gearman. Thoughts? http://docs.php.net/manual/en/gearman.examples-reverse-bg.php
  14. Howdy, we are writing a PHP script which creates virtual machines via a RESTful API call. That part is quite easy. Once that request to create the VM is sent to the server, the API request returns with essentially "Machine queued to be created...". When we create a virtual machine, we insert a record into a MySQL database basically with VM label, and DATE-CREATED-STARTED. That record also has a field DATE-CREATED-FINISHED which is NULL. LABEL DATE-CREATED-STARTED DATE-CREATED-FINISHED test-vm-1 2011-05-14 12:00:00 NULL So here is our problem. How do we basically spin/spawn off a PHP worker, on the initial request, that checks the status of the queued virtual machine every 10 seconds, and when the virtual machine is up and running, updates DATE-CREATED-FINISHED. Keep in mind, the initial API request immediately returns "Machine queue to be created." and then exits. The PHP worker needs to be doing the 10 second check in the background. Thanks.
  15. Discovered something very strange with trim() and numbers with leading zeros. <?php $number = 0456; echo trim($number); ?> That ouputs 302. How the heck does 0456 trimmed turn into 302? Is this a bug?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.