2017-04-06 2 views
0

私が書いたコードに問題があります。私はコードを何回かテストしたが、うまくいきましたが、問題は、ある特定のサーバー(Namecheap)でこのPHPファイルを実行すると、正しく動作しない関数がいくつかあり、別のサーバー(1and1)それは100%良くなります。私は自分の市場を確立するためにIPに基づいて国名を取得したいと思いますが、それを行うコードがありますが、Namecheapでは$ country_nameは表示されませんが、1and1ではcertenlyします。これはPHPのバージョンと関係があるのだろうか?皆さんのおかげで、いくつかのドキュメントを参照して、自分で解決することができるかどうかを確認できます。PHPの国名は表示されません

私は十分にはっきりしていることを願っています。 ありがとうございます。あなたがいないfile_get_content魔女が本当にWebページの結果を取得していないカールの機能を使用する必要がありますipinfoドキュメントよると

<?php 

/* ---------------------------------------/
* XX. IP and country (Market). 
* -------------------------------------- */ 

// Gets client's IP. 
$ip = getenv("HTTP_CLIENT_IP")?: 
getenv("HTTP_X_FORWARDED_FOR")?: 
getenv("HTTP_X_FORWARDED")?: 
getenv("HTTP_FORWARDED_FOR")?: 
getenv("HTTP_FORWARDED")?: 
getenv("REMOTE_ADDR"); 

// Matchs gotten IP with API values and retrives location value. 
$location = json_decode(file_get_contents("http://ipinfo.io/{$ip}")); 

// "Country code to name" array. 
$countries = array("BD" => "Bangladesh", "BE" => "Belgium", "BF" => "Burkina Faso", "BG" => "Bulgaria", "BA" => "Bosnia and Herzegovina", "BB" => "Barbados", "WF" => "Wallis and Futuna", "BL" => "Saint Barthelemy", "BM" => "Bermuda", "BN" => "Brunei", "BO" => "Bolivia", "BH" => "Bahrain", "BI" => "Burundi", "BJ" => "Benin", "BT" => "Bhutan", "JM" => "Jamaica", "BV" => "Bouvet Island", "BW" => "Botswana", "WS" => "Samoa", "BQ" => "Bonaire, Saint Eustatius and Saba ", "BR" => "Brazil", "BS" => "Bahamas", "JE" => "Jersey", "BY" => "Belarus", "BZ" => "Belize", "RU" => "Russia", "RW" => "Rwanda", "RS" => "Serbia", "TL" => "East Timor", "RE" => "Reunion", "TM" => "Turkmenistan", "TJ" => "Tajikistan", "RO" => "Romania", "TK" => "Tokelau", "GW" => "Guinea-Bissau", "GU" => "Guam", "GT" => "Guatemala", "GS" => "South Georgia and the South Sandwich Islands", "GR" => "Greece", "GQ" => "Equatorial Guinea", "GP" => "Guadeloupe", "JP" => "Japan", "GY" => "Guyana", "GG" => "Guernsey", "GF" => "French Guiana", "GE" => "Georgia", "GD" => "Grenada", "GB" => "United Kingdom", "GA" => "Gabon", "SV" => "El Salvador", "GN" => "Guinea", "GM" => "Gambia", "GL" => "Greenland", "GI" => "Gibraltar", "GH" => "Ghana", "OM" => "Oman", "TN" => "Tunisia", "JO" => "Jordan", "HR" => "Croatia", "HT" => "Haiti", "HU" => "Hungary", "HK" => "Hong Kong", "HN" => "Honduras", "HM" => "Heard Island and McDonald Islands", "VE" => "Venezuela", "PR" => "Puerto Rico", "PS" => "Palestinian Territory", "PW" => "Palau", "PT" => "Portugal", "SJ" => "Svalbard and Jan Mayen", "PY" => "Paraguay", "IQ" => "Iraq", "PA" => "Panama", "PF" => "French Polynesia", "PG" => "Papua New Guinea", "PE" => "Peru", "PK" => "Pakistan", "PH" => "Philippines", "PN" => "Pitcairn", "PL" => "Poland", "PM" => "Saint Pierre and Miquelon", "ZM" => "Zambia", "EH" => "Western Sahara", "EE" => "Estonia", "EG" => "Egypt", "ZA" => "South Africa", "EC" => "Ecuador", "IT" => "Italy", "VN" => "Vietnam", "SB" => "Solomon Islands", "ET" => "Ethiopia", "SO" => "Somalia", "ZW" => "Zimbabwe", "SA" => "Saudi Arabia", "ES" => "Spain", "ER" => "Eritrea", "ME" => "Montenegro", "MD" => "Moldova", "MG" => "Madagascar", "MF" => "Saint Martin", "MA" => "Morocco", "MC" => "Monaco", "UZ" => "Uzbekistan", "MM" => "Myanmar", "ML" => "Mali", "MO" => "Macao", "MN" => "Mongolia", "MH" => "Marshall Islands", "MK" => "Macedonia", "MU" => "Mauritius", "MT" => "Malta", "MW" => "Malawi", "MV" => "Maldives", "MQ" => "Martinique", "MP" => "Northern Mariana Islands", "MS" => "Montserrat", "MR" => "Mauritania", "IM" => "Isle of Man", "UG" => "Uganda", "TZ" => "Tanzania", "MY" => "Malaysia", "MX" => "Mexico", "IL" => "Israel", "FR" => "France", "IO" => "British Indian Ocean Territory", "SH" => "Saint Helena", "FI" => "Finland", "FJ" => "Fiji", "FK" => "Falkland Islands", "FM" => "Micronesia", "FO" => "Faroe Islands", "NI" => "Nicaragua", "NL" => "Netherlands", "NO" => "Norway", "NA" => "Namibia", "VU" => "Vanuatu", "NC" => "New Caledonia", "NE" => "Niger", "NF" => "Norfolk Island", "NG" => "Nigeria", "NZ" => "New Zealand", "NP" => "Nepal", "NR" => "Nauru", "NU" => "Niue", "CK" => "Cook Islands", "XK" => "Kosovo", "CI" => "Ivory Coast", "CH" => "Switzerland", "CO" => "Colombia", "CN" => "China", "CM" => "Cameroon", "CL" => "Chile", "CC" => "Cocos Islands", "CA" => "Canada", "CG" => "Republic of the Congo", "CF" => "Central African Republic", "CD" => "Democratic Republic of the Congo", "CZ" => "Czech Republic", "CY" => "Cyprus", "CX" => "Christmas Island", "CR" => "Costa Rica", "CW" => "Curacao", "CV" => "Cape Verde", "CU" => "Cuba", "SZ" => "Swaziland", "SY" => "Syria", "SX" => "Sint Maarten", "KG" => "Kyrgyzstan", "KE" => "Kenya", "SS" => "South Sudan", "SR" => "Suriname", "KI" => "Kiribati", "KH" => "Cambodia", "KN" => "Saint Kitts and Nevis", "KM" => "Comoros", "ST" => "Sao Tome and Principe", "SK" => "Slovakia", "KR" => "South Korea", "SI" => "Slovenia", "KP" => "North Korea", "KW" => "Kuwait", "SN" => "Senegal", "SM" => "San Marino", "SL" => "Sierra Leone", "SC" => "Seychelles", "KZ" => "Kazakhstan", "KY" => "Cayman Islands", "SG" => "Singapore", "SE" => "Sweden", "SD" => "Sudan", "DO" => "Dominican Republic", "DM" => "Dominica", "DJ" => "Djibouti", "DK" => "Denmark", "VG" => "British Virgin Islands", "DE" => "Germany", "YE" => "Yemen", "DZ" => "Algeria", "US" => "United States", "UY" => "Uruguay", "YT" => "Mayotte", "UM" => "United States Minor Outlying Islands", "LB" => "Lebanon", "LC" => "Saint Lucia", "LA" => "Laos", "TV" => "Tuvalu", "TW" => "Taiwan", "TT" => "Trinidad and Tobago", "TR" => "Turkey", "LK" => "Sri Lanka", "LI" => "Liechtenstein", "LV" => "Latvia", "TO" => "Tonga", "LT" => "Lithuania", "LU" => "Luxembourg", "LR" => "Liberia", "LS" => "Lesotho", "TH" => "Thailand", "TF" => "French Southern Territories", "TG" => "Togo", "TD" => "Chad", "TC" => "Turks and Caicos Islands", "LY" => "Libya", "VA" => "Vatican", "VC" => "Saint Vincent and the Grenadines", "AE" => "United Arab Emirates", "AD" => "Andorra", "AG" => "Antigua and Barbuda", "AF" => "Afghanistan", "AI" => "Anguilla", "VI" => "U.S. Virgin Islands", "IS" => "Iceland", "IR" => "Iran", "AM" => "Armenia", "AL" => "Albania", "AO" => "Angola", "AQ" => "Antarctica", "AS" => "American Samoa", "AR" => "Argentina", "AU" => "Australia", "AT" => "Austria", "AW" => "Aruba", "IN" => "India", "AX" => "Aland Islands", "AZ" => "Azerbaijan", "IE" => "Ireland", "ID" => "Indonesia", "UA" => "Ukraine", "QA" => "Qatar", "MZ" => "Mozambique"); 

// Assigns the location value to the variable "&country_code". 
$country_code = $location->country; 

// Holds country name value after checking array. 
$country_name = $countries[$country_code]; 

// Posted data. 
$posted_data["market"] = $country_name; 

// Holds IP value. 
$posted_data["ip-address"] = $ip; 

/* End of IP and country (Market) */ 

echo 'IP address: ' . $ip . '<br>Country: ' . $country_name; 
+0

'getenv'の代わりに' $ _SERVER'を試しましたか? – jotaelesalinas

+0

国名はレスポンスに含まれていません。 Juanraの答えを調べて、それを取得する方法を確認してください。 –

+0

いいえ、私は@jotaelesalinasはありませんでしたが、私はします。これは私を夢中にさせている! – KAZZABE

答えて

1

:サーバ上のファイルの

リンクはNamecheapここで

1and1は私のPHPコードであるここにありますjsonデータ。ここで

curl ipinfo.io/8.8.8.8 
{ 
    "ip": "8.8.8.8", 
    "hostname": "google-public-dns-a.google.com", 
    "loc": "37.385999999999996,-122.0838", 
    "org": "AS15169 Google Inc.", 
    "city": "Mountain View", 
    "region": "California", 
    "country": "US", 
    "phone": 650 
} 

Documentation here

PHPのカールを持つ例:私は、これはあなたを助けることを願って

<?php 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io/8.8.8.8"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true); 

$result=curl_exec($ch); 

//print_r($result); 

$jsonResult = json_decode($result); 

echo "Country: ".$jsonResult->country; 

curl_close($ch); 
?> 

+0

ねえ! @Juanra私はこれを昨日見ましたが、真実は私がカールについての手掛かりを持っていないということです。だからもし私に言えばリファレンスガイド。 – KAZZABE

+0

どういう意味ですか...私はこれをどのように実装できますか? – KAZZABE

+0

こんにちは! @kaZZABEここで見ているように、あなたのスクリプトにcurl関数を使う必要があります:http://php.net/manual/es/function.curl-exec.php(例があります)非常に便利な関数です。 – Juanra

0

APIリンクが無効です。使用方法:別のIPの詳細については、独自のIP

http://ipinfo.io/8.8.8.8/jsonため

http://ipinfo.io/json

あなたが唯一の国での使用についての情報を取得したい場合:

http://ipinfo.io/8.8.8.8/country(JSONのない、プレーンテキストのみ)

あなたのコード:

$ip = getenv("REMOTE_ADDR"); //or something else ip 
$country_code = file_get_contents("http://ipinfo.io/$ip/country"); //plain country code, no json 

(...)

// Holds country name value after checking array. 
$country_name = $countries[$country_code]; 

もちろん8.8.8.8正しいアドレスに変更

+0

私はこれを試してみるつもりですが、私はまだ質問があります...なぜ私の元のコードが動作し、1and1サーバーですか? – KAZZABE

+0

これはテスト済みですか?それは私のために働かなかった、@ 1と1が動作していません。 : – KAZZABE

+0

@kaZZABEこれらのサーバ(1and1とNamecheap)のコードは同じですか? – rafalli

0

@ juanraの回答から、次のコードが完成しました。

<?php 

/* ---------------------------------------/
* XX. IP and country (Market). 
* -------------------------------------- */ 

// Gets client's IP. 
$ip = getenv("HTTP_CLIENT_IP")?: 
getenv("HTTP_X_FORWARDED_FOR")?: 
getenv("HTTP_X_FORWARDED")?: 
getenv("HTTP_FORWARDED_FOR")?: 
getenv("HTTP_FORWARDED")?: 
getenv("REMOTE_ADDR"); 

// CURL init. Matchs gotten IP with API values and retrives location value. 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io/{$ip}"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true); 
// json output. 
$output = curl_exec($ch); 
// CURL close. 
curl_close($ch); 

// "Country code to name" array. 
$countries = array("BD" => "Bangladesh", "BE" => "Belgium", "BF" => "Burkina Faso", "BG" => "Bulgaria", "BA" => "Bosnia and Herzegovina", "BB" => "Barbados", "WF" => "Wallis and Futuna", "BL" => "Saint Barthelemy", "BM" => "Bermuda", "BN" => "Brunei", "BO" => "Bolivia", "BH" => "Bahrain", "BI" => "Burundi", "BJ" => "Benin", "BT" => "Bhutan", "JM" => "Jamaica", "BV" => "Bouvet Island", "BW" => "Botswana", "WS" => "Samoa", "BQ" => "Bonaire, Saint Eustatius and Saba ", "BR" => "Brazil", "BS" => "Bahamas", "JE" => "Jersey", "BY" => "Belarus", "BZ" => "Belize", "RU" => "Russia", "RW" => "Rwanda", "RS" => "Serbia", "TL" => "East Timor", "RE" => "Reunion", "TM" => "Turkmenistan", "TJ" => "Tajikistan", "RO" => "Romania", "TK" => "Tokelau", "GW" => "Guinea-Bissau", "GU" => "Guam", "GT" => "Guatemala", "GS" => "South Georgia and the South Sandwich Islands", "GR" => "Greece", "GQ" => "Equatorial Guinea", "GP" => "Guadeloupe", "JP" => "Japan", "GY" => "Guyana", "GG" => "Guernsey", "GF" => "French Guiana", "GE" => "Georgia", "GD" => "Grenada", "GB" => "United Kingdom", "GA" => "Gabon", "SV" => "El Salvador", "GN" => "Guinea", "GM" => "Gambia", "GL" => "Greenland", "GI" => "Gibraltar", "GH" => "Ghana", "OM" => "Oman", "TN" => "Tunisia", "JO" => "Jordan", "HR" => "Croatia", "HT" => "Haiti", "HU" => "Hungary", "HK" => "Hong Kong", "HN" => "Honduras", "HM" => "Heard Island and McDonald Islands", "VE" => "Venezuela", "PR" => "Puerto Rico", "PS" => "Palestinian Territory", "PW" => "Palau", "PT" => "Portugal", "SJ" => "Svalbard and Jan Mayen", "PY" => "Paraguay", "IQ" => "Iraq", "PA" => "Panama", "PF" => "French Polynesia", "PG" => "Papua New Guinea", "PE" => "Peru", "PK" => "Pakistan", "PH" => "Philippines", "PN" => "Pitcairn", "PL" => "Poland", "PM" => "Saint Pierre and Miquelon", "ZM" => "Zambia", "EH" => "Western Sahara", "EE" => "Estonia", "EG" => "Egypt", "ZA" => "South Africa", "EC" => "Ecuador", "IT" => "Italy", "VN" => "Vietnam", "SB" => "Solomon Islands", "ET" => "Ethiopia", "SO" => "Somalia", "ZW" => "Zimbabwe", "SA" => "Saudi Arabia", "ES" => "Spain", "ER" => "Eritrea", "ME" => "Montenegro", "MD" => "Moldova", "MG" => "Madagascar", "MF" => "Saint Martin", "MA" => "Morocco", "MC" => "Monaco", "UZ" => "Uzbekistan", "MM" => "Myanmar", "ML" => "Mali", "MO" => "Macao", "MN" => "Mongolia", "MH" => "Marshall Islands", "MK" => "Macedonia", "MU" => "Mauritius", "MT" => "Malta", "MW" => "Malawi", "MV" => "Maldives", "MQ" => "Martinique", "MP" => "Northern Mariana Islands", "MS" => "Montserrat", "MR" => "Mauritania", "IM" => "Isle of Man", "UG" => "Uganda", "TZ" => "Tanzania", "MY" => "Malaysia", "MX" => "Mexico", "IL" => "Israel", "FR" => "France", "IO" => "British Indian Ocean Territory", "SH" => "Saint Helena", "FI" => "Finland", "FJ" => "Fiji", "FK" => "Falkland Islands", "FM" => "Micronesia", "FO" => "Faroe Islands", "NI" => "Nicaragua", "NL" => "Netherlands", "NO" => "Norway", "NA" => "Namibia", "VU" => "Vanuatu", "NC" => "New Caledonia", "NE" => "Niger", "NF" => "Norfolk Island", "NG" => "Nigeria", "NZ" => "New Zealand", "NP" => "Nepal", "NR" => "Nauru", "NU" => "Niue", "CK" => "Cook Islands", "XK" => "Kosovo", "CI" => "Ivory Coast", "CH" => "Switzerland", "CO" => "Colombia", "CN" => "China", "CM" => "Cameroon", "CL" => "Chile", "CC" => "Cocos Islands", "CA" => "Canada", "CG" => "Republic of the Congo", "CF" => "Central African Republic", "CD" => "Democratic Republic of the Congo", "CZ" => "Czech Republic", "CY" => "Cyprus", "CX" => "Christmas Island", "CR" => "Costa Rica", "CW" => "Curacao", "CV" => "Cape Verde", "CU" => "Cuba", "SZ" => "Swaziland", "SY" => "Syria", "SX" => "Sint Maarten", "KG" => "Kyrgyzstan", "KE" => "Kenya", "SS" => "South Sudan", "SR" => "Suriname", "KI" => "Kiribati", "KH" => "Cambodia", "KN" => "Saint Kitts and Nevis", "KM" => "Comoros", "ST" => "Sao Tome and Principe", "SK" => "Slovakia", "KR" => "South Korea", "SI" => "Slovenia", "KP" => "North Korea", "KW" => "Kuwait", "SN" => "Senegal", "SM" => "San Marino", "SL" => "Sierra Leone", "SC" => "Seychelles", "KZ" => "Kazakhstan", "KY" => "Cayman Islands", "SG" => "Singapore", "SE" => "Sweden", "SD" => "Sudan", "DO" => "Dominican Republic", "DM" => "Dominica", "DJ" => "Djibouti", "DK" => "Denmark", "VG" => "British Virgin Islands", "DE" => "Germany", "YE" => "Yemen", "DZ" => "Algeria", "US" => "United States", "UY" => "Uruguay", "YT" => "Mayotte", "UM" => "United States Minor Outlying Islands", "LB" => "Lebanon", "LC" => "Saint Lucia", "LA" => "Laos", "TV" => "Tuvalu", "TW" => "Taiwan", "TT" => "Trinidad and Tobago", "TR" => "Turkey", "LK" => "Sri Lanka", "LI" => "Liechtenstein", "LV" => "Latvia", "TO" => "Tonga", "LT" => "Lithuania", "LU" => "Luxembourg", "LR" => "Liberia", "LS" => "Lesotho", "TH" => "Thailand", "TF" => "French Southern Territories", "TG" => "Togo", "TD" => "Chad", "TC" => "Turks and Caicos Islands", "LY" => "Libya", "VA" => "Vatican", "VC" => "Saint Vincent and the Grenadines", "AE" => "United Arab Emirates", "AD" => "Andorra", "AG" => "Antigua and Barbuda", "AF" => "Afghanistan", "AI" => "Anguilla", "VI" => "U.S. Virgin Islands", "IS" => "Iceland", "IR" => "Iran", "AM" => "Armenia", "AL" => "Albania", "AO" => "Angola", "AQ" => "Antarctica", "AS" => "American Samoa", "AR" => "Argentina", "AU" => "Australia", "AT" => "Austria", "AW" => "Aruba", "IN" => "India", "AX" => "Aland Islands", "AZ" => "Azerbaijan", "IE" => "Ireland", "ID" => "Indonesia", "UA" => "Ukraine", "QA" => "Qatar", "MZ" => "Mozambique"); 

// Assigns ISO Alpha-2 value to the variable "&country_code". 
$country_code = json_decode($output, true); 

// Assigns "$country_name" value after checking array. 
$country_name = $countries[$country_code["country"]]; 

/* End of IP and country (Market) */ 

echo 'The country code is: ' . $country_code["country"] . ' and your country is: ' . $country_name . '. Your IP address is: ' . $ip; 

?> 

これは100%動作しています。おかげでJuanra!

関連する問題