ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I'm trying to convert this script into Perl: http://ultradev.com.nmsrv.com/custadd-nojson.txt

The syntax of this is a little bit foreign to me, so any pointers would be much appreciated :)

From what I can tell, I would use something like LWP::Simple to send the request as a POST, but I'm a bit stumped with how I would format this kinda stuff:
"Customer": { "Name": "'.$customer['name'].'", "Website": "'.$customer['website'].'", "Address": "'.$customer['address'].'", "City": "'.$customer['city'].'", "StateProvince": "'.$customer['stateprovince'].'", "Country": "United States", "ZipPostalCode": "'.$customer['zippostalcode'].'", "PrimaryContact": { "Salutation": "'.$customer['salutation'].'", "FirstName": "'.$customer['first name'].'", "LastName": "'.$customer['last name'].'", "Title": "'.$customer['title'].'", "Email": "'.$customer['email'].'", "PhoneBusiness": { "Number":"'.$customer['phone'].'"} } '.$customer_fields.' }
Any suggestions would be much appreciated :)

TIA

Andy

Replies are listed 'Best First'.
Re: PHP -> Perl question
by Khen1950fx (Canon) on Sep 02, 2010 at 09:52 UTC
    You would use something like LWP::UserAgent.
    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use PHP::Include; include_php_vars('file.php');
    PHP::Include makes it really easy. You just need a little file.php
    #file.php <?php /* * Add customer example code * Uses PHP 5 but not JSON * */ $GLOBALS['api_url'] = "https://demo.rpmsoftware.com/rpm/Api.svc/"; $GLOBALS['api_key'] = "xxxxx"; /* * An array with the customer data to add * */ $customer = array( 'name' => '111A Test Customer1', 'website' => '', 'address' => '', 'city' => '', 'stateprovince' => '', 'zippostalcode' => '', 'salutation' => '', 'first name' => '', 'last name' => '', 'title' => '', 'email' => '', 'phone' => '', // To use custom fields make 'fields' an array like this: // 'fields' => array( // 'fieldname' => 'fieldvalue' // ); // // Example: // 'fields' => array( // 'Custom text' => 'a demo value', // 'Some date' => '12/20/2009', // ); 'fields' => '' ); /* * A generic POST function * * This isn't specific to RPM, it's just a simple POST wrapper to * fopen. Any post function will do and something using CURL is * probably better: http://curl.haxx.se/libcurl/php/ * */ function req_post($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'POST', 'content' => $data )); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url"); } return $response; } /* * API call without JSON functions * */ function rpm_req($call, $data_string = "") { if ("" != $data_string) $data_string = ", ".$data_string; // Build the request $data = '{"Key":"'.$GLOBALS['api_key'].'" '.$data_string.' }'; // Make the request $result = req_post($GLOBALS['api_url'].$call, $data, 'Content-Type +: application/json; charset=utf-8'); // Analyse the results $response_pattern = '/\{\"[A-Za-z]+\":\"(.+)\"\}/'; $error_pattern = '/\{\\\"Error\\\":\\\"([A-za-z0-9 ]+)\\\"\}/'; if (preg_match($response_pattern, $result, $matches)) { $return = $matches[1]; if (preg_match($error_pattern, $return, $matches)) { throw new Exception("API returned error: $matches[1]"); } return $return; } else { throw new Exception("Problem with return from $url"); } } ?> <html> <head> <title>Add customer API example</title> <style> body { background: #eee; font-family: Arial, sans-serif; font-size +: small; } </style> </head> <body> <?php if (is_array($customer['fields'])) { $customer_fields = ', "Fields": ['; foreach ($customer['fields'] as $field_name => $field_value) { $customer_fields .= '{"Field":"'.$field_name.'","Value":"'.$fi +eld_value.'"},'; } $customer_fields = substr($customer_fields, 0, strlen($customer_fi +elds) - 1); // chop off the last , $customer_fields .= ']'; } else $customer_fields = ""; try { $result = rpm_req('CustomerAdd',' "Customer": { "Name": "'.$customer['name'].'", "Website": "'.$customer['website'].'", "Address": "'.$customer['address'].'", "City": "'.$customer['city'].'", "StateProvince": "'.$customer['stateprovince'].'", "Country": "United States", "ZipPostalCode": "'.$customer['zippostalcode'].'", "PrimaryContact": { "Salutation": "'.$customer['salutation'].'", "FirstName": "'.$customer['first name'].'", "LastName": "'.$customer['last name'].'", "Title": "'.$customer['title'].'", "Email": "'.$customer['email'].'", "PhoneBusiness": { "Number":"'.$customer['phone'].'"} } '.$customer_fields.' } '); echo 'Customer added'; } catch (Exception $e) { echo $e->getMessage(); } ?> </body> </html>
      Hi,

      Ah nice - I didn't realise you could call the PHP scripts like that from a perl script :) Will get my host to install that module, and then see if I can get it going!

      Thanks :)

      Andy
        While you're at it, you might as well use JSON for proper serialization
Re: PHP -> Perl question
by Anonymous Monk on Sep 02, 2010 at 09:39 UTC
    So you're not understanding the PHP? Me neither, though in the original it looks pretty much like perl
    try { $result = rpm_req('CustomerAdd',' "Customer": { "Name": "'.$customer['name'].'", "Website": "'.$customer['website'].'", "Address": "'.$customer['address'].'", "City": "'.$customer['city'].'", "StateProvince": "'.$customer['stateprovince'].'", "Country": "United States", "ZipPostalCode": "'.$customer['zippostalcode'].'", "PrimaryContact": { "Salutation": "'.$customer['salutation'].'", "FirstName": "'.$customer['first name'].'", "LastName": "'.$customer['last name'].'", "Title": "'.$customer['title'].'", "Email": "'.$customer['email'].'", "PhoneBusiness": { "Number":"'.$customer['phone'].'"} } '.$customer_fields.' } '); echo 'Customer added'; } catch (Exception $e) { echo $e->getMessage(); }
    becomes
    eval { $result = rpm_req( 'CustomerAdd', ' "Customer": { "Name": "' . $customer{'name'} . '", "Website": "' . $customer{'website'} . '", "Address": "' . $customer{'address'} . '", "City": "' . $customer{'city'} . '", "StateProvince": "' . $customer{'stateprovince'} . '", "Country": "United States", "ZipPostalCode": "' . $customer{'zippostalcode'} . '", "PrimaryContact": { "Salutation": "' . $customer{'salutation'} . '", "FirstName": "' . $customer{'first name'} . '", "LastName": "' . $customer{'last name'} . '", "Title": "' . $customer{'title'} . '", "Email": "' . $customer{'email'} . '", "PhoneBusiness": { "Number":"' . $customer{'phone'} . '"} } ' . $customer_fields . ' } ' ); } or warn $@;