WWW ::卷曲::易输出无法捕捉

问题描述:

use JSON; 
use WWW::Curl::Easy; 

my $curl = WWW::Curl::Easy->new; 
$curl->setopt(CURLOPT_NOBODY,1); 
$curl->setopt(CURLOPT_TIMEOUT,3); 
$curl->setopt(CURLOPT_VERBOSE,0); 
$curl->setopt(CURLOPT_URL,$url); 


my $response = (what to use here); 

my %hash = decode_json($response); 
$country = $hash->{body}->{country}; 
return $country; 


#the output from url is in json format. 
#but unable to capture that output in a response variable as object. 

我试图从$url这是JSON格式的变量,然后使用JSON的decode_json方法将其转换成散列结构以获得国家代码捕获效应初探。但我无法在变量中获得响应。WWW ::卷曲::易输出无法捕捉

JSON Output: 
{ 
    "headers": { 
     "ipAddress": ["198.162.1.1"], 
     "type": ["PUBLIC_IP_ADDRESS"] 
    }, 
    "body": { 
     "country": { 
      "isoCode": "CA", 
      "name": "Canada", 
      "geoNameId": 6251999 
     }, 
     "continent": { 
      "name": "North America", 
      "code": "NA", 
      "geoNameId": 6255149 
     }, 
     "city": { 
      "name": "Cran*", 
      "geoNameId": 5931800 
     }, 
     "traits": { 
      "ipAddress": "198.162.1.1" 
     }, 
     "location": { 
      "latitude": 49.4999, 
      "longitude": -115.7688, 
      "timeZone": "America/Edmonton" 
     }, 
     "postal": { 
      "code": "V1C" 
     }, 
     "subdivisions": [{ 
      "name": "British Columbia", 
      "geoNameId": 5909050, 
      "isoCode": "BC" 
     }] 
    }, 
    "statusCode": "OK" 
} 
200 
+0

[WWW :: Curl :: Easy](https://metacpan.org/pod/WWW::Curl)的文档说这并不容易。它的目的不是为了成为一个独立的模块,正因为如此,应该参考http://curl.haxx.se_中有关API的详细信息来查阅主要的libcurl文档。你读过吗?我相信你会通过使用[LWP :: Simple](http://p3rl.org/LWP::Simple)来改变你的生活。 – simbabque

+0

@simbabque:是的,我已经使用LWP做了同样的事情,但根据需要,我需要使用WWW :: Curl :: Easy模块来实现此功能。这里是输出json:{“headers”:{“ipAddress”:[“192.2.2.2”],“type”:[“PUBLIC_IP_ADDRESS”]},“body”:{“country”:{“isoCode”:“US “,”name“:”美国“,”geoNameId“:6252001},”continent“:{”name“:”北美“,”code“:”NA“,”geoNameId“:6255149} :{}, “特征”:{ “ip地址”: “192.2.2.2”}, “位置”:{ “纬度”:37.751, “东经”: - 97.822}, “邮政”:{}} “的StatusCode” :“确定”} –

+0

JSON是如何相关的?您知道如何解码JSON以及如何访问其中的密钥,这在问题中很明显。 – simbabque

This seems that WWW::Curl::Easy require file handle. 
Look at bold lines 


#!/usr/bin/perl 

use WWW::Curl::Easy; 
use Data::Dumper ; 

my $resp_body =""; 

my $curl=WWW::Curl::Easy->new; 

$curl->setopt(CURLOPT_URL,$url); 

# Define file handle and send it to string 
open (my $fh, ">", \$resp_body); 

$curl->setopt(CURLOPT_WRITEDATA,\$fh); 

my $ret_code =$curl->perform; 

if ($ret_code ==0){ 
print "Response : $resp_body"; 
}else{ 
print "Error".$curl->errbuf ; 
} 
close($fh); 

这在WWW::Curl文档中的第一个示例代码块中进行了说明。我在这里将它与来自问题的代码一起转载。

my $curl = WWW::Curl::Easy->new; 
$curl->setopt(CURLOPT_URL,$url); 

# A filehandle, reference to a scalar or reference to a typeglob can be used here. 
my $response_body; 
$curl->setopt(CURLOPT_WRITEDATA,\$response_body); 

# Starts the actual request 
my $retcode = $curl->perform; 

my %hash = decode_json($response_body); 
$country = $hash->{body}->{country}; 

您需要设置某种引用作为CURLOPT_WRITEDATA选项。我们使用了一个标量引用,变量$response_body将在$curl->perform完成后包含响应主体。然后您可以解码JSON。

+0

@ KartikParihar似乎是一个内部URL。我无法找到它的DNS记录。 '$ response_body'中有什么?它里面有什么吗? – simbabque

+1

$ response_body仍为空。但是当我运行$ curl->执行它打印输出到控制台作为JSON字符串如上所示。 –

+0

@Kartik哦。我不清楚这一点。您应该编辑您的问题并添加该信息。在这个评论中,它很难阅读。有几种方法可以捕获STDOUT,但这并不是真的解决问题,而是解决方法。你把'\'放在'WRITEDATA'的变量前面了吗? – simbabque

经过多次与本期的WWW :: Curl :: Easy拒绝打印到变量,并不断写入STDOUT,我终于找到了问题!

要从WWW-卷曲4.17和WWW 得不到你的WWW ::卷曲::易::卷曲::易在WWW-卷曲3.02找到。

所以,简单地通过整个WWW卷曲4.17包覆盖以前的WWW ::卷曲::安装方便:

http://search.cpan.org/~szbalint/WWW-Curl-4.17/lib/WWW/Curl.pm

然后,它会服从CURLOPT_WRITEDATA。实际上,其他Curl甚至不知道CURLOPT_WRITEDATA,因为您将通过使用严格添加来实现;