Exemple #1
0
 /**
  * @return Response
  */
 public function countriesAction()
 {
     die('nope');
     $em = $this->getDoctrine()->getManager();
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, 'http://api.geonames.org/countryInfoJSON?username=nebs');
     http:
     //api.geonames.org/countryInfoJSON?username=nebs&lang=hr
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
     // Assuming you're requesting JSON
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $response = curl_exec($ch);
     // If using JSON...
     $data = json_decode($response);
     foreach ($data->geonames as $country) {
         $name = $country->countryName;
         $code = $country->countryCode;
         $countryObj = new Country();
         $countryObj->setName($name);
         $countryObj->setCode($code);
         $em->persist($countryObj);
         $em->flush();
     }
     return $this->render('AdminBundle::parse.html.twig', array('data' => $data->geonames));
 }
 protected function createAndPersistData()
 {
     $countryCount = 0;
     foreach ($this->countries as $code => $country) {
         $countryCount++;
         $countryEntity = new Country();
         $countryEntity->setCode($code)->setName($country[0])->setCurrency($country[1]);
         $this->setReference(sprintf('country_%s', $countryCount), $countryEntity);
         $this->manager->persist($countryEntity);
     }
 }