<?php// Verileri okuyan ve şifresini çözen fonksiyonfunction oku($fileName, $secretKey, $encryptionMethod){$fileContent = file_get_contents($fileName);$fileData = json_decode($fileContent, true);$iv = base64_decode($fileData['iv']);$decryptedData = openssl_decrypt(base64_decode($fileData['data']), $encryptionMethod, $secretKey, 0, $iv);return json_decode($decryptedData, true);}// Verileri yazan ve şifreleyen fonksiyonfunction yaz($fileName, $data, $secretKey, $encryptionMethod){$jsonData = json_encode($data);$ivLength = openssl_cipher_iv_length($encryptionMethod);$iv = openssl_random_pseudo_bytes($ivLength);$encryptedData = openssl_encrypt($jsonData, $encryptionMethod, $secretKey, 0, $iv);$encryptedFileContent = json_encode(['data' => base64_encode($encryptedData),'iv' => base64_encode($iv),]);file_put_contents($fileName, $encryptedFileContent);}//VERİTABANI BAĞLANTI$fileName = 'lisanslar.txt'; // tablo adı$secretKey = "Alp123*_.";$encryptionMethod = "AES-256-CBC";//OKUMA - SELECT$data = oku($fileName, $secretKey, $encryptionMethod);print_r($data);//YAZMA - INSERT$data[] = ["name" => "Alperen", "price" => 1000];writeData($fileName, $data, $secretKey, $encryptionMethod);//SİLME - DELETE$id = 0; //dizi index nounset($data[$id]);$data = array_values($data);writeData($fileName, $data, $secretKey, $encryptionMethod);//GÜNCELLEME - UPDATE$id = 0;//güncellenecek veri id$name= "Mehmet"; //yeni isim$data[$id]['name'] = $name;writeData($fileName, $data, $secretKey, $encryptionMethod);
Etiket: JSON
Json API oluşturma ve kullanımı
JSON (“Javascript Object Notation”), php ilen okunabilen bir veri biçimidir. Php’de bir diziden
<?php$dizi=array(1=>"elma",2=>"armut");echo json_encode($dizi);?>
örneğindeki gibi json_encode ile dönüştürülerek oluşturulur.
Çıktısı aşağıdaki gibidir.
{1: "elma",2: "armut"}
Genelde API (“Application Programming Interface = Uygulama Programlama Arayüzü”)ler tarafından kullanılır.
Curl ile bağlanılan adreslerde alınan bu veriler json_decode ile geri dizi haline çevrilir.
<?php //Daha önceki verimiz$dizi=array(1=>"elma",2=>"armut");$API= json_encode($dizi);//Nesne ve Çıktısı$cikti=json_decode($API);print_r($cikti);//stdClass Object ( [1] => elma [2] => armut )//Dizi ve Çıktısı$cikti=json_decode($API,true);print_r($cikti);//Array ( [1] => elma [2] => armut )?>
Mesela İstatistik API ile ilgili bir örnek verelim
<?phpheader('Content-Type: text/html; charset=utf-8');function PhpStatistics(){$list=array("HTTP_HOST","REQUEST_URI","HTTP_CLIENT_IP","REMOTE_ADDR","HTTP_USER_AGENT","HTTP_ACCEPT_LANGUAGE");foreach($list as $l){$data[$l]=$_SERVER[$l];}$data=json_encode($data);$ch = curl_init();curl_setopt($ch,CURLOPT_HEADER,false);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible;)");curl_setopt($ch,CURLOPT_URL,"http://phpstate.ulusanyazilim.com/developer/userinfo/api.php?".http_build_query(array('data'=>$data)));return curl_exec($ch);}$json=PhpStatistics();$dizi=json_decode($json);print_r($dizi);echo $tarayici=$dizi["agent_name"]."<br>";echo $ip_adresi=$dizi["http_client_ip"];?>
Youtube Oembed JSON API
<?php$url = "http://www.youtube.com/watch?v=CvBfHwUxHIk";//burası değişken$oembedurlcreate = "http://www.youtube.com/oembed?url=".$url."&format=json";$curl = curl_init($oembedurlcreate);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$html=curl_exec($curl);curl_close($curl);$result = json_decode($html, true);echo $result["thumbnail_url"]."<br>";//http://i.ytimg.com/vi/CvBfHwUxHIk/hqdefault.jpgecho $result["provider_url"]."<br>";//http://www.youtube.com/echo $result["thumbnail_height"]."<br>";//360echo $result["type"]."<br>";//videoecho $result["provider_name"]."<br>";//YouTubeecho $result["version"]."<br>";// 1.0echo $result["html"]."<br>";//html5 video oynacıtıcını görüntülerecho $result["author_name"]."<br>";//RihannaVEVOecho $result["width"]."<br>";//459echo $result["title"]."<br>";//Rihanna - Umbrella (Orange Version) ft. JAY-Zecho $result["height"]."<br>";//344echo $result["thumbnail_width"]."<br>";//480echo $result["author_url"]."<br>";//http://www.youtube.com/user/RihannaVEVO?>