code_junkie

What's the simplest way to make a HTTP GET request in Perl?

Nov 11th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /**
  2. * Makes a remote call to the our API, and returns the response
  3. * @param cmd {string} - command string ID
  4. * @param argsArray {array} - associative array of argument names and argument values
  5. * @return {array} - array of responses
  6. */
  7. function callAPI( $cmd, $argsArray=array() )
  8. {
  9. $apikey="MY_API_KEY";
  10. $secret="MY_SECRET";
  11. $apiurl="https://foobar.com/api";
  12.  
  13. // timestamp this API was submitted (for security reasons)
  14. $epoch_time=time();
  15.  
  16. //--- assemble argument array into string
  17. $query = "cmd=" .$cmd;
  18. foreach ($argsArray as $argName => $argValue) {
  19. $query .= "&" . $argName . "=" . urlencode($argValue);
  20. }
  21. $query .= "&key=". $apikey . "&time=" . $epoch_time;
  22.  
  23. //--- make md5 hash of the query + secret string
  24. $md5 = md5($query . $secret);
  25. $url = $apiurl . "?" . $query . "&md5=" . $md5;
  26.  
  27. //--- make simple HTTP GET request, put the server response into $response
  28. $response = file_get_contents($url);
  29.  
  30. //--- convert "|" (pipe) delimited string to array
  31. $responseArray = explode("|", $response);
  32. return $responseArray;
  33. }
  34.  
  35. use LWP::Simple;
  36. $contents = get("http://YOUR_URL_HERE");
  37.  
  38. use LWP::Simple;
  39. $content = get($url);
  40. die "Can't GET $url" if (! defined $content);
Add Comment
Please, Sign In to add comment