Advertisement
Guest User

Untitled

a guest
Dec 14th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. <?php
  2.  
  3. $check = checkCityOrCountry('Cana');
  4. if($check === false)
  5. echo 'Not Valid country';
  6. else
  7. {
  8. echo 'Valid: <pre>';
  9. print_r($check);
  10. echo '</pre>';
  11. }
  12.  
  13. function checkCityOrCountry($name)
  14. {
  15. $checkCity = $name;
  16. $checkCity = mb_strtolower($checkCity, "UTF-8");
  17.  
  18. $countries = vkapi('database.getCountries', array(
  19. 'need_all' => 1,
  20. 'count' => 1000), null, true);
  21. $countries = $countries['response'];
  22.  
  23. $validString = false;
  24.  
  25. $cCnt = count($countries);
  26. for($i = 0; $i < $cCnt; ++$i)
  27. {
  28. $title = mb_strtolower($countries[$i]['title'], "UTF-8");
  29. if(mb_strpos($title, $checkCity, 0, 'UTF-8') !== false)
  30. {
  31. $validString = $countries[$i];
  32. break;
  33. }
  34.  
  35. /*search by cities too, but extremely long*/
  36. // $cities = vkapi('database.getCities', array(
  37. // 'country_id' => $countries[$i]['cid'],
  38. // 'q' => $checkCity,
  39. // 'count' => 1000), null, true);
  40. // $cities = $cities['response'];
  41. // if(count($cities) > 0)
  42. // {
  43. // $validString = $cities;
  44. // break;
  45. // }
  46. }
  47.  
  48. return $validString;
  49. }
  50. /**
  51. * @function vkapi Perform a request to api VK
  52. * @param string $method Name of method
  53. * @param array $params Post parameters
  54. * @param string $token Secure token if you need it
  55. * @param boolean $array If = true, will returns an array, in other case - an object
  56. * @return array Result
  57. */
  58. function vkapi($method, $params = array(), $token=null, $array=false) {
  59. try
  60. {
  61. $rid = 0;
  62. if(isset($token))
  63. $params["access_token"] = $token;
  64.  
  65. $params['lang'] = 'en';
  66. $paramstr = http_build_query($params);
  67. $url = "https://api.vk.com/method/" . $method . "?" . $paramstr;
  68.  
  69. $ch = curl_init($url);
  70. curl_setopt($ch, CURLOPT_HEADER, 0);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  72. $result = json_decode(curl_exec($ch));
  73.  
  74. if($array == true)
  75. {
  76. $result = json_decode(json_encode($result), true);
  77. }
  78.  
  79. return $result;
  80. }
  81. catch(Exception $e)
  82. {
  83. throw new Exception('VK API: '.$e->getMessage());
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement