пятница, 20 ноября 2009 г.

Create the Google Docs Form

http://www.brighthub.com/hubfolio/matthew-casperson/articles/56351.aspx

First up we need to create a Google Form to accept the data. All you need to do is create a Form that has 5 paragraph fields. For this demo we will use this form, which I have created in my own Google Docs account.

Get the POST back URL

In order to post data to the form, we need to know the URL that the HTML form posts back to. You can get this by looking at the HTML code, and looking at the form action attribute.

For the form above, this post back address is http://spreadsheets.google.com/formResponse?formkey=dDM1U21NM3hvck56SGJ4N3U4elJocFE6MA&ifq.

Google Docs Forms and PHP Screenshot

Post data to the form using PHP and CURL

The exception_handler function takes an Exception object and then sends its details to the Google Form.

function exception_handler($exception)

{

First up we define the post back URL that was found in the HTML source code for the Google Form.

$formURL = "http://spreadsheets.google.com/formResponse?formkey=dDM1U21NM3hvck56SGJ4N3U4elJocFE6MA&ifq";

Now we create an array that maps the exception details to the fields of the Google Form. Notice that the Google Form fields follow a consistent naming system in the format entry.#.single.

$fields = array(

'entry.0.single'=>rawurlencode($exception->getMessage()),

'entry.1.single'=>rawurlencode($exception->getCode()),

'entry.2.single'=>rawurlencode($exception->getFile()),

'entry.3.single'=>rawurlencode($exception->getLine()),

'entry.4.single'=>rawurlencode($exception->getTraceAsString()),

);

These fields needs to be converted into the format option1=value1&option2=value2 before they can be sent in a HTTP POST operation.

foreach($fields as $key=>$value)

{

$fields_string .= $key.'='.$value.'&';

}

rtrim($fields_string,'&');

The final step is to use CURL to send the data to the Google Form.

$session = curl_init();

curl_setopt($session, CURLOPT_URL, $formURL);

curl_setopt($session, CURLOPT_POST, 1);

curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);

curl_exec($session);

curl_close($session);

}

Generate an exception

We set the set_exception_handler function set the exception_handler function to handle all uncaught PHP exceptions. To test the code we then throw an exception.

set_exception_handler('exception_handler');

throw new Exception('Uncaught Exception');

Комментариев нет:

Отправить комментарий