Joshua's Thoughts

quickbase php api examples

Tags: , , , , ,

i’ve had a few requests for examples of how to use the quickbase php api wrapper. so here goes:

setup the quickbase object with login info

include the library and setup the object by passing in username, password, true/false (xml/http), and dbID of the db/table you’ll be transacting with.

include_once('../includes/qb.php');
$quickbase = new QuickBase('username','password', true, 'dbID');

do a quickbase query

here, we setup a query to return field 3 where (field id 15 is equal to somevalue).

$queries = array(
			array(
				'fid'	=> '15',
				'ev'	=> 'ex',
				'cri'	=> 'somevalue')
			 );

$results = $quickbase->do_query($queries, '', '', '3');

adding a record to a quickbase

this one is super easy. create an associative array with field id’s and values and then use the add_record method.

$fields = array(
			array(
				'fid'	=> '1',
				'value'	=> 'some value'),
			array(
				'fid'	=> '2',
				'value'	=> 'some value'),
			array(
				'fid'	=> '3',
'value'=>'some value')
						  );
$quickbase->add_record($fields);

edit a quickbase record

almost identical to add_record, setup the array and use the add_record method.

$fields = array(
			array(
				'fid'	=> '1',
				'value'	=> 'some value'),
			array(
				'fid'	=> '2',
				'value'	=> 'some value'),
			array(
				'fid'	=> '3',
'value'=>'some value')
						  );
$quickbase->edit_record($fields);

do a quickbase query

here, we setup a query to return field 3 where (field id 15 is equal to somevalue).

$queries = array(
			array(
				'fid'	=> '15',
				'ev'	=> 'ex',
				'cri'	=> 'somevalue')
			 );

$results = $quickbase->do_query($queries, '', '', '5');

view realtime requests per second from log

Tags: , ,

so you want to view the number of requests to your webserver per second. here’s a quick and easy php script that you can run from command line.

  while(true){
    echo exec('cat /var/log/httpd/access_log | wc -l') . "\n";
    sleep(1);
  }

it isn’t perfect, but it will work for doing some basic troubleshooting. just hit ctrl+c to terminate.

and if you want to simulate a bunch of request so you can watch the num go up and down, here’s a script to help:

while(true){
  exec('wget yourdomain.com');
  unlink('index.html');
}
?>

csv to associative array

Tags: , , , ,

i’ve been working a major integration project with quickbase and vtrenz. one of the things i had to do this week was take a ~10K row csv, scrub the cells for specific length, and then ftp to a remote server.

so i wrote a php script that would connect to quickbase, pull down the csv, write it to file, open it, turn the csv into an array, scrub it, recreate the csv from the array, write it to file again, and then send it on it’s merry way ala ftp.

all of it was easy except turning the csv into an array. i didn’t want to step through each character looking for newlines or my delimeter (a comma) and i needed something that would use the first row of the csv as the column headings.

i was able to find some scripts on the web that half-way worked. in fact, the one i used before writing my own worked, it was just super slow and inefficient. it literally used a gig of memory for 5K records.

so here’s my solution for taking a very large csv, and using the first row to create a key=>value associative array.

function buildStock($File, $rID) {
  $handle = fopen($File, "r");
  $fields = fgetcsv($handle, 5000, ",");

  $y = 0;
  while($data = fgetcsv($handle, 5000, ",")) {
    $x = 0;
    foreach($data as $value) {
      $stock[$y][$fields[$x]] = $value;
        $x++;
     }
     $y++;
    }
   return $stock;
}

it now only takes a few seconds to turn a considerably large csv into an array. hope this helps someone out there.

How to Pass Arguments to PHP in Command Line

Tags: , , , , ,

I did a little bit of searching this morning for how to pass arguments to php via command line because I was working on setting up a php script to run on a cron in Windows (using WAMP).

It’s actually pretty easy. Say your command line looks like this:

>path-to-php/php.exe path-to-script/script.php

Passing an argument to the php script would look like this:

>path-to-php/php.exe path-to-script/script.php arg1 arg2

PHP will read the arguments using the $argv variable. So in our example, $argv[0] would be the name of the script (script.php), $argv[1] would be arg1 and $argv2[2] would be arg2.

Another example:
>path-to-php/php.exe path-to-script/script.php 15

Say you just wanted to echo out the argument contents:

echo $argv[1];

Pretty cool. Here’s the forum post where I discovered this information.

how to enable libcurl in wamp

Tags: , , ,

if you’re getting “Fatal error: Call to undefined function: curl_init()” and you’re using wamp, you’ve got to perform a few extra steps to get curl working.

wamp comes with a crap load of extensions, including curl. it’s just a matter of getting the extension loaded when wamp is started. and no, enabling php_curl from within the wamp menu is not how to do it. for some reason, that just doesn’t work.

here’s what you need to do:

  • open C:\wamp\bin\php\php5.2.6\php.ini
  • find “;extension=php_curl.dll” and remove the semicolon to uncomment the line
  • do the same for C:\wamp\bin\apache\apache2.2.8\bin\php.ini

it doesn’t matter what version of wamp you’re running at the time, the process is the same.

next, you need to make sure apache can find the extension to load, so make sure that in both php.ini files, you find the lines:

; Directory in which the loadable extensions (modules) reside.
extension_dir = “C:\wamp\bin\php\php5.2.6\ext”

… and have them both point to the ext folder within your PHP folder. by default, they will be something like /usr/bin/ext. change that.

restart wamp and it should all work. you can look in the phpinfo to see if curl was loaded. if it still doesn’t work, check the apache error log within the wamp logs folder.