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": "Cranbrook",
"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
[WWW :: Curl :: Easy](https://metacpan.org/pod/WWW::Curl)的文檔說這並不容易。它的目的不是爲了成爲一個獨立的模塊,正因爲如此,應該參考http://curl.haxx.se_中有關API的詳細信息來查閱主要的libcurl文檔。你讀過嗎?我相信你會通過使用[LWP :: Simple](http://p3rl.org/LWP::Simple)來改變你的生活。 – simbabque
@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」 :「確定」} –
JSON是如何相關的?您知道如何解碼JSON以及如何訪問其中的密鑰,這在問題中很明顯。 – simbabque