CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help translating a few lines of PHP into Perl?

 



tim594
New User

Sep 11, 2008, 1:11 PM

Post #1 of 2 (644 views)
Help translating a few lines of PHP into Perl? Can't Post

The first person to respond with a working translation gets $20 over paypal

This is for an API for a project im working on.
Would someone be so kind as to help me translate this over from PHP.
I need it in Perl, and eventually Python...




Code
/** 
* Makes a remote call over GET/HTTPS
* See example call below on how it might be used.
* @param cmd {string} - a Command string ID
* @argsArray {array} - associative array of argument names and argument values
* @return {array} - array of responses
*/
function callAPI( $cmd, $argsArray )
{
$apikey="MY_API_KEY";
$secret="MY_SECRET";
$apiurl="https://domain.com/api";
$epoch_time=time();

//--- assemble argument array into string
$query = "cmd=$cmd"
foreach ($argsArray as $argName=>$argValue) {
$query .= "&" . $argName . "=" . urlencode($argValue);
}
$query .= "&key=$apikey&time=$epoch_time";

//--- an example $query var would look like this:
//--- "cmd=loginUser&email=my@address.com ...
//--- &pw=pass&key=MY_API_KEY&time=1221166373"

//--- make md5 hash of the query + secret string
$md5 = md5($query . $secret);
$url = "$apiurl?$query&md5=$md5";

//--- simple GET request, put its contents into $response
$response = file_get_contents($url);

//--- the reponse is a "|" (pipe) delimited string,
//--- which we will convert to array
//--- example string response:
//--- "OK|USER_ID_HERE|User was logged in successfully"
$responseArray = explode("|", $response,);
return $responseArray;
}

//--- example usage:
$response=callAPI("loginUser", array("email"=>"my@address.com", "pw"=>"pass"));

$responseCode = $response[0];
$userID = $response[1];
$message = $response[2];



For those not familiar with PHP:
- The "." (period) in the $md5 line just concats the $query and $secret strings
- $var is a variable, and when placed in double quotes $var will be replaced with its value,
eg. $var="world"; echo "Hello $var"

Thanks!!

(This post was edited by tim594 on Sep 11, 2008, 2:26 PM)


scrpnsanctuary
Novice

Oct 7, 2008, 3:23 PM

Post #2 of 2 (471 views)
Re: [tim594] Help translating a few lines of PHP into Perl? [In reply to] Can't Post

I'll take a crack at some of it.

No guarantees I do it the best way, or that it's perfect, though. =)



Code
# 
# Makes a remote call over GET/HTTPS
# See example call below on how it might be used.
# @param cmd {string} - a Command string ID
# @argsArray {array} - associative array of argument names and argument values
# @return {array} - array of responses
#

sub callAPI
{

### ADD HERE
$cmd = shift(@_); ### Get first argument passed for $cmd
%argsHash = @_; ### Get rest of arguments passed as hash -- not 100% sure on this line

### Changed to single quotes for literals
$apikey='MY_API_KEY';
$secret='MY_SECRET';
$apiurl='https://domain.com/api';
$epoch_time=time();

##--- assemble argument array into string
$query = "cmd=$cmd";

### Changed foreach statement, perl-ized
foreach $key (keys %argsHash)
{
### I've had problems depending on build using the '.' for concatenation in perl, so i like to rely on join()
$query = join('', $query, '&', $key , '=' , urlencode($argsHash{$key});
}
$query = join('', $query, "\&key=$apikey\&time=$epoch_time");

##--- an example $query var would look like this:
##--- "cmd=loginUser&email=my@address.com ...
##--- &pw=pass&key=MY_API_KEY&time=1221166373"

##--- make md5 hash of the query + secret string
$md5 = md5(join('', $query, $secret));
$url = "$apiurl\?$query\&md5=$md5";

##--- simple GET request, put its contents into $response
$response = file_get_contents($url);

##--- the reponse is a "|" (pipe) delimited string,
##--- which we will convert to array
##--- example string response:
##--- "OK|USER_ID_HERE|User was logged in successfully"
@responseArray = split (/\|/, $response);
return (@responseArray);
}

##--- example usage:

### Defining has separately, just as a precaution
%params =("email"=>"my@address.com", "pw"=>"pass");

$response=callAPI("loginUser", %params);

$responseCode = $response[0];
$userID = $response[1];
$message = $response[2];

----------
The vastness of what we know is only surpassed by the vastness of what we don't.

(This post was edited by scrpnsanctuary on Oct 7, 2008, 3:30 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives