在Perl中使用HTTP GET请求的最简单的方法是什么?
发布时间:2020-12-15 21:24:30 所属栏目:大数据 来源:网络整理
导读:我有一些代码,我写在PHP中消耗我们简单的webservice,我也想提供给Perl的用户可能更喜欢这种语言。什么是最简单的方法来做一个HTTP请求呢?在PHP中,我可以在一行中使用file_get_contents()。 这里是我想要移植到Perl的整个代码: /** * Makes a remote cal
我有一些代码,我写在PHP中消耗我们简单的webservice,我也想提供给Perl的用户可能更喜欢这种语言。什么是最简单的方法来做一个HTTP请求呢?在PHP中,我可以在一行中使用file_get_contents()。
这里是我想要移植到Perl的整个代码: /** * Makes a remote call to the our API,and returns the response * @param cmd {string} - command string ID * @param argsArray {array} - associative array of argument names and argument values * @return {array} - array of responses */ function callAPI( $cmd,$argsArray=array() ) { $apikey="MY_API_KEY"; $secret="MY_SECRET"; $apiurl="https://foobar.com/api"; // timestamp this API was submitted (for security reasons) $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; //--- make md5 hash of the query + secret string $md5 = md5($query . $secret); $url = $apiurl . "?" . $query . "&md5=" . $md5; //--- make simple HTTP GET request,put the server response into $response $response = file_get_contents($url); //--- convert "|" (pipe) delimited string to array $responseArray = explode("|",$response); return $responseArray; } 解决方法
LWP ::简单:
use LWP::Simple; $contents = get("http://YOUR_URL_HERE"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- hdu 1171 Big Event in HDU【生成函数】
- [bigdata-106] spring-cloud-03 创建一个hello服务并把它注
- 命令行 – 可以使用Inno Setup接受自定义命令行参数
- java – org.springframework.beans.NotWritableProperty
- Power BI基础系列-05.可视化设计
- 用perl来改写shell
- CodeForces 1095B Array Stabilization
- Delphi中使用Office中VBA的优缺点
- Perl脚本在终端中运行,但在从Java程序调用时不运行
- 为什么Perl打印文件句柄的方式是这样的?