Jump to content

atrum

Members
  • Posts

    184
  • Joined

  • Last visited

Everything posted by atrum

  1. So it has recently occurred to me that my method of error handling is sloppy and cumbersome. What I want to do is record all of the errors that occur and allow the page to finish loading. Then display them in a designated error panel on my page. Granted that if I come across a major error I do want to stop the script but for minor errors such as incorrect credentials or some other custom error I may want to display I want to neatly display them all without stopping the script. Can anyone offer any advice?
  2. Oh yeah I forgot I still had this old link up. I fixed it here http://icca.exiled-alliance.com. I changed the name of it so that it was more general. I still haven't put anything in those tabs. Kinda lost interest in this thing but it gets a lot of use at my work place. Every time my web server goes down, I get spammed by people wanting to know when it would be fixed haha.
  3. So there really is no right or wrong way to use them then other than trying to keep them primarily for Setter use. Cool, thank you Thorpe.
  4. So, I recently discovered what method chaining is and how it can be used. My question about method chaining is in regards to best practice. What I want to know is what the correct way to implement methods that can be chained are, and what pitfalls will I need to watch out for when using them?
  5. Here is the solution I came up with using Curl instead of Fsockopen. <?php //Curl Test Function define("HOST","api.host.com"); //API HOST define("ACCT_INFO","/account/information/1.0/"); //Defines the api path for getting account and product details. define("KEY_ID","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); //Defines key identification code. define("SECRET_KEY","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); //Defines secret response. function hmacsha1($secret, $string){ $blocksize = 64; $hashfunc = 'sha1'; if (strlen($secret) > $blocksize) $secret = pack('H*', $hashfunc($secret)); $secret = str_pad($secret,$blocksize,chr(0x00)); $ipad = str_repeat(chr(0x36),$blocksize); $opad = str_repeat(chr(0x5c),$blocksize); $hmac = pack('H*',$hashfunc(($secret^$opad).pack('H*',$hashfunc(($secret^$ipad).$string)))); return $hmac; } function fetch_info($customer,$action,$method){ if($customer!==""){ if($action!==""){ $accept = 'application/json'; // Assign json application $proxy = "https://".HOST.$action.$customer; // Assign the proxy (URL/URI) $path = HOST.$action.$customer; // Authorization Header $date = gmdate('r'); // GMT based timestamp $string = join("\n", array("GET", $accept, $date, $proxy)); // Join string with new lines $signature = base64_encode(hmacsha1(SECRET_KEY, $string)); // Create authorization signature $ch = curl_init(); //Initialize the Curl Session. //Set all the necessary headers to authenticate. $headers = array( "$method $path HTTP/1.1", "Host: ".HOST, "Accept: $accept", "Content-Type: $accept", "Authorization: VKEY ".KEY_ID.":$signature", "Date: $date", "Connection: Close" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //Feeds the header details into the request. curl_setopt($ch, CURLOPT_HEADER, 0); //Mostly for debugging. if 3rd argument is 1, include header response. curl_setopt($ch, CURLOPT_URL, $proxy); //Sets the URL/PATH. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Allows returned data to be stored in a variable for later use. $results = json_decode(curl_exec($ch), true); //Returned data in an associative array. curl_close($ch); //Close the curl session return $results; //Returns an array of data requested. }else{ $error = "Missing Argument: [Action]"; return $error; } }else{ $error = "Missing Argument: [Customer ID]"; return $error; } } ?>
  6. Hey man, thanks a lot for the help on that. That is a much cleaner way of doing this.
  7. I got it figured out. So far it works out exactly the same when I manually work out the numbers. <?php //Initialize Variables if($_POST['num1']!=="" || $_POST['num2']!==""){ $num1 = $_POST['num1']; $num2 = $_POST['num2']; }else{ die("Both fields require a number"); } //Function calulates the Greatest Common Factor from 2 given numbers. function gcd($num1,$num2){ while($num1 !== $num2){ if($num1 > $num2){ //While $num1 is equal $num2 $num1 = $num1 - $num2; //Subtract $num1 from $num2 $gcd = $num1; }else{ $num2 = $num2 - $num1; //Subtract $num1 from $num2 $gcd = $num2; } if($gcd <=1){ //Checks if the gcd is lower than 1. if it is, then stop the loop and make gcd equal to the number before we hit 0 $gcd = $num1; break; } } $result = "The GCD of ".$_POST['num1'] ." and ". $_POST['num2']." is : ".$gcd; return $result; } echo gcd($_POST['num1'],$_POST['num2']); ?>
  8. Hello everyone. So in my programing class which is starting out doing flow charts. We were asked to do a flow chart that shows the process of calculating the GCD from 2 numbers. I wanted to try it out for real in php. The issue I am having is that after the first time its used, any attempt to use it after that results in a never ending loop that ends up tanking my server, and I have to restart apache to fix the problem. I am trying to come up with a solution to prevent that but I am drawing a blank. Looping is still somewhat new to me. At least when dealing with just numbers. Here is my code,. If anyone can offer any advice I would appreciate it. <?php //Initialize Variables if($_POST['num1']!=="" || $_POST['num2']!==""){ $num1 = $_POST['num1']; $num2 = $_POST['num2']; }else{ die("Both fields require a number"); } //Function calulates the Greatest Common Factor from 2 given numbers. function gcd($num1,$num2){ while($num1 !== $num2){ if($num1 > $num2){ //While $num1 is equal $num2 $num1 = $num1 - $num2; //Subtract $num1 from $num2 $gcd = $num1; }else{ $num2 = $num2 - $num1; //Subtract $num1 from $num2 $gcd = $num2; } } $result = "The GCD of ".$_POST['num1'] ." and ". $_POST['num2']." is : ".$gcd; return $result; } echo gcd($_POST['num1'],$_POST['num2']); ?>
  9. Hello all, I am working on a project that has a function in it that I don't completely understand. From what I can understand about it; it appears to create a hash from 2 strings. The parts I don't understand is the methods it is going about it with. Here is the function. If someone could please break this down for me into its parts with a brief explanation of what each part is doing, I would be very appreciative. <?php function hmacsha1($secret, $string){ $blocksize = 64; $hashfunc = 'sha1'; if (strlen($secret) > $blocksize) $secret = pack('H*', $hashfunc($secret)); $secret = str_pad($secret,$blocksize,chr(0x00)); $ipad = str_repeat(chr(0x36),$blocksize); $opad = str_repeat(chr(0x5c),$blocksize); $hmac = pack('H*',$hashfunc(($secret^$opad).pack('H*',$hashfunc(($secret^$ipad).$string)))); return $hmac; } ?>
  10. Yo, Gizmola. I got this all working through curl now. So much easier than fsockopen. Thanks again for your help!
  11. I actually got it figured out. I think that example is very out of date or something. I don't even need the fopen function to make it work. I have a new issue now, but I will make a new post since it's not related to curl or fopen. Thanks though. Here is basically what I did. <?php $ch = curl_init(); //Initialize the Curl Session. $headers = array( "GET $path HTTP/1.1", "Host: $host", "Accept: $accept", "Content-Type: $accept", "Authorization: VKEY $keyid:$signature", "Date: $date", "Connection: Close" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, $proxy); $results = curl_exec($ch); curl_close($ch); ?>
  12. hello all, I am trying to teach my self how to use curl to get the contents of a remote file. I am using the example provided on the php.net website under the curl example. <?php //Curl driven verio api test $ch = curl_init("http://curtisdorris.com/"); //Initialize the Curl Session. $fp = fopen("index.php","r"); //Open the file for reading only curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); ?> I am getting the following warnings and I am unable to find any information as to why I am getting them. It still works but I really want to take care of these warnings before I continue further. Warning: fopen(index.php) [function.fopen]: failed to open stream: No such file or directory in /home/UID/www/tools.exiled-alliance.com/apitest/curltest.php on line 4 Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/UID/www/tools.exiled-alliance.com/apitest/curltest.php on line 6 Can anyone offer any in-sight as to wtf is going on?
  13. Awesome. I will let you know if I need any help. Thanks.
  14. The authentication required is over www-auth. Can curl or fopen do that? I've only used curl and fopen. At there most basic
  15. Ok here are the functions I use to connect to the api server. I am using fsockopen to request the information. I am pretty lost when it comes to regex so I don't really have anything to show that I have tried. Most of the time I get individual characters in an array instead of words. <?php // VSAPI Config define("API_HOST","api.host.com"); //API HOST define("ACCESS_KEY_ID","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //API Acess Key ID define("SECRET_ACCESS_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //API Secret Access Key $key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //vsapi_connect: establishes the connection to the VSAPI server. //3 arguments: $key, and $secret from the backroom api interface. function vsapi_connect($host,$key,$secret,$string){ if($key !=="" || $string !==""){//if $key, or $secret are not blank, and the $string_array is an array. $fp = fsockopen("ssl://".$host, 443, $errno, $errstr, 30); if(!$fp){ return $errno ." : ".$errstr; }else{ return $fp; } }else{ return false; } } //hmacsha1: hashing.// I don't know exactly what this does, but it is needed. function hmacsha1($secret, $string){ $blocksize = 64; $hashfunc = 'sha1'; if(strlen($secret) > $blocksize) $secret = pack('H*', $hashfunc($secret)); $secret = str_pad($secret,$blocksize,chr(0x00)); $ipad = str_repeat(chr(0x36),$blocksize); $opad = str_repeat(chr(0x5c),$blocksize); $hmac = pack('H*',$hashfunc(($secret^$opad).pack('H*',$hashfunc(($secret^$ipad).$string)))); return $hmac; } //request: sends the request data over the connect, and returns the response. function request($fp, $q){ fwrite($fp, $q); $r = ''; while(!feof($fp)){ $tr = fgets($fp, 256); $r .= $tr; } return $r; } ?> And here is the code that im using to call the process. <?php require_once($_SERVER['DOCUMENT_ROOT']."/apitest/api_connect.php"); //Connection settings and functions. $accept = "application/json"; $verb = "GET"; //Method $path = "/api/script/path"; //URI path $proxy = "https://".API_HOST.$path; //HOST + URI $date = gmdate('r'); //Date formated for httpd header. $string = join("\n",array($verb,$accept,$date,$proxy)); $signature = base64_encode(hmacsha1(SECRET_ACCESS_KEY, $string));// Create authorization signature $fp = vsapi_connect(API_HOST, ACCESS_KEY_ID, SECRET_ACCESS_KEY, $string); // Request $query = $verb." ".$path." HTTP/1.1\r\n"; // HTTP verb $query .= "Host: api.host.com\r\n"; // Host header $query .= "Accept: ".$accept."\r\n"; // Accept header $query .= "Content-Type: ".$accept."\r\n"; // Content-type header $query .= "Authorization: VKEY ".$key.":".$signature."\r\n"; // Authorization header $query .= "Date: ".$date."\r\n"; // Date header $query .= "Connection: Close\r\n\r\n"; // Connection header $response = request($fp, $query); // Communicate with API thru URL/URI echo $response; // This is the part that contains the json string ?>
  16. Hello everyone, I am working with an API that sends responses in a json format, but unfortunately it also sends raw header information in the same response. Json_decode isn't working I assume because of this extra data its not expecting. So unless there is another way I was trying to use regex to separate the 2 data types. Basically the format I get is this. HTTP/1.1 200 OK Date: Mon, 06 Jun 2011 20:25:38 GMT Server: Apache/2.0.64 (Red Hat) Vary: Content-Type Content-Length: 603 Connection: close Content-Type: application/json {"WARNINGS":[],"DATA":{"key:value","key:value","key:value","key:value","key:value","key:value","key:value","key:value"}]},"ERRORS":[]} It all gets sent as a single string. Can anyone help me figure this out?
  17. Thanks man, appreciate the help.
  18. Thanks for the reply teynon. On that first part that says SELECT c.* how is it that it knows that I want all columns from contacts when it seems c is assigned later in the query. Or like php does the order not matter?
  19. Hello everyone, I am working on a project that uses an sql query that is a little more advanced than what I have been exposed to in the past. some I understand most of it but it has some parts I don't really grasp. Could any one break this down for me into parts and explain what it is this is doing? SELECT c.* , (SELECT COUNT( * ) FROM `referrals` AS r WHERE r.`referrals_contact_id` = c.`contacts_id` ) AS gl_count FROM `contacts` AS c WHERE c.`contacts_demo_date` >= '" . $fromDateDB . "' AND c.`contacts_demo_date` <= '" . $toDateDB . "' AND `" . $_REQUEST['queryColumn'] . "` = " . $answer . ";
  20. I got my start at w3schools.com. I know they do have some bad information, but their php section at least teaches you the bare bones basics.
  21. Self taught over here The only way to learn is to do it. I started out making simple html forms that spit out what I typed into them and then I started securing those forms, connecting to databases, and before I knew it I was applying for php development jobs.
  22. Thanks again man, this makes a lot more sense now. Yet another example of how my brain can over complicate something that is in actuality really simple.
  23. Man I love getting replies from you Ignace. You know how to speak my language. I never even imagined that something I have been doing pretty much from day 1 is the MVC pattern. Thanks
  24. Hello all, So I am trying to become familiar with the concept of the MVC (Model View Control) design pattern. I think I get the concept but I tend to need an example in its most basic practical form to understand something well enough to actually make use of it. I was wondering if anyone could provide an example and an explanation with out a lot of theory. (I have plenty of books on the theory is why but very little on real life application). Thanks, Atrum
×
×
  • 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.