Here are steps how can upload a file in Bunny – Storage.
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
include 'bunnycdn.php'; $cdn = new BunnyCDN(); // FTP Paasword $storage_key='0123b910-4349-4faf-83a6ff9cgg06-ec17-40f2'; $local_upload_file_path = 'image.jpg'; $storage_zone_file_path = '/image.jpg'; $storage_zone_path="/storagezone"; $d1 = $cdn->Storage($storage_key)->PutFile($local_upload_file_path, $storage_zone_path, $storage_zone_file_path); |
bunnycdn.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
<?php class BunnyCDN { private $api_key_account; private $api_key_storage; protected $api_url = array ( "zone" => "https://bunnycdn.com/api", 'storage' => 'https://storage.bunnycdn.com' ); //--->account > start public function Account($api_key_account='') { if(!$api_key_account) { return array('status' =>'error' ,'code' =>'missing_api_key_account' ,'msg'=> 'missing api key account'); die(); } $this->api_key_account = $api_key_account; return $this; } public function GetZoneList() { /* will get all of the zones for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone'; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , ) ); if($api_call['http_code'] !=200) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array)->Message , ); return $result; die(); } $zone_data = json_decode($api_call['data']); $a1 = array(); foreach ($zone_data as $k1 => $v1) { $arr_hostnames = array(); //--->get all the hostnames > start if($v1->Hostnames) { foreach ($v1->Hostnames as $key => $v2) { array_push($arr_hostnames, $v2->Value); } } //--->get all the hostnames > end $d = array ( "zone_id" => $v1->Id, "zone_name"=>$v1->Name, "monthly_bandwidth_used" =>$this->format_bytes($v1->MonthlyBandwidthUsed), "host_names" =>$arr_hostnames, ); array_push($a1,$d); } return array('status' => 'success', 'zone_smry'=>$a1,"zone_details" => $zone_data); } public function GetZone($zone_id = '') { /* will get a user zone for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/'.$zone_id; $get_header = $this->create_header($key); $post_data_array = array('id'=>$zone_id); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) ); if($api_call['http_code'] !=200) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } $zone_data = json_decode($api_call['data']); $a1 = array(); $arr_hostnames = array(); //--->get all the hostnames > start if($zone_data->Hostnames) { foreach ($zone_data->Hostnames as $key => $v1) { array_push($arr_hostnames, $v1->Value); } } //--->get all the hostnames > end $d = array ( "zone_id" => $zone_data->Id, "zone_name"=>$zone_data->Name, "monthly_bandwidth_used" =>$this->format_bytes($zone_data->MonthlyBandwidthUsed), "host_names" =>$arr_hostnames, ); array_push($a1,$d); return array('status' => 'success', 'zone_smry'=>$a1,"zone_details" => $zone_data); die(); } public function CreateNewZone($zone_name = '', $zone_url = '') { /* will create a new zone for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_name) { return array('status' =>'error' ,'code' =>'zone_name' ,'msg'=> 'missing zone name'); die(); } if(!$zone_url) { return array('status' =>'error' ,'code' =>'zone_url' ,'msg'=> 'missing zone url'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone'; $get_header = $this->create_header($key); $post_data_array = array('Name' => $zone_name, 'OriginUrl' => $zone_url); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) ); if($api_call['http_code'] !=201) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } //convert to php array for data parsing $zone_data = json_decode($api_call['data']); //--->get all the hostnames > start $cdnurl = ''; if($zone_data->Hostnames) { foreach ($zone_data->Hostnames as $key => $v1) { $cdnurl = $v1->Value; } } //--->get all the hostnames > end return array ( 'status' => 'success', "zone_id" => $zone_data->Id, "zone_name"=>$zone_data->Name, "origin_url"=>$zone_data->OriginUrl, "cdn_url"=>$cdnurl, "zone_details" => $zone_data ); die(); } public function DeleteZone($zone_id= '') { /* will delete a zone for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/'. $zone_id; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'DELETE', 'api_url' => $api_url,'header' => $get_header , ) ); if($api_call['http_code'] !=200 && $api_call['http_code'] !=302) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); //return $api_call; die(); } public function PurgeZoneCache($zone_id= '') { /* will purge cache for the whole zone */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/'. $zone_id.'/purgeCache'; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , ) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function AddHostName($zone_id = '', $host_name_url = '') { /* will add a host name for the zone */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } if(!$host_name_url) { return array('status' =>'error' ,'code' =>'host_name_url' ,'msg'=> 'missing host name url'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/addHostname'; $get_header = $this->create_header($key); $post_data_array = array('PullZoneId' => $zone_id, 'Hostname' => $host_name_url); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function DeleteHostName($zone_id = '', $host_name_url = '') { /* will delete a host name for the zone */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } if(!$host_name_url) { return array('status' =>'error' ,'code' =>'host_name_url' ,'msg'=> 'missing host name url'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/deleteHostname?id='.$zone_id.'&hostname='.$host_name_url ; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'DELETE', 'api_url' => $api_url,'header' => $get_header , ) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function AddBlockedIP($zone_id = '', $blocked_ip = '') { /* will add a blocked ip for the zone */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } if(!$blocked_ip) { return array('status' =>'error' ,'code' =>'blocked_ip' ,'msg'=> 'missing blocked ip'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/addBlockedIp' ; $get_header = $this->create_header($key); $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function RemoveBlockedIP($zone_id = '', $blocked_ip = '') { /* will remove a blocked ip for the zone */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$zone_id) { return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id'); die(); } if(!$blocked_ip) { return array('status' =>'error' ,'code' =>'blocked_ip' ,'msg'=> 'missing blocked ip'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/pullzone/removeBlockedIp' ; $get_header = $this->create_header($key); $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function PurgeURL($url = '') { /* will purge a url for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$url) { return array('status' =>'error' ,'code' =>'url' ,'msg'=> 'missing url'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/purge?url='.$url ; $get_header = $this->create_header($key); //$post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , )); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } public function Stats() { /* will get all the statistics for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/statistics'; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , )); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => json_decode( ($api_call['data'])), ); die(); } public function Billing() { /* will get the billing information for the account */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/billing'; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , )); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => json_decode( ($api_call['data'])), ); die(); } public function ApplyCode($apply_code = '') { /* will apply a promo code to account to save money */ if( !$this->api_key_account) { return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key'); die(); } if(!$apply_code) { return array('status' =>'error' ,'code' =>'apply_code' ,'msg'=> 'missing apply code'); die(); } $key = $this->api_key_account; $api_url = $this->api_url['zone'].'/billing/applycode?couponCode='.$apply_code ; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , )); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', "msg" => $api_call, ); die(); } //--->account > end //--->storage > start public function Storage($api_key_storage='') { if(!$api_key_storage) { return array('status' =>'error' ,'code' =>'api_key_storage' ,'msg'=> 'missing storage api key'); die(); } $this->api_key_storage = $api_key_storage; return $this; } public function GetStorageZone($storage_path ='') { /* will get all of the files and subfolders for storage zone */ if( !$this->api_key_storage) { return array('status' =>'error' ,'code' =>'api_key_storage' ,'msg'=> 'missing storage api key'); die(); } if(!$storage_path ) { return array('status' =>'error' ,'code' =>'missing_zone_id' ,'msg'=> 'missing zone id'); die(); } $key = $this->api_key_storage; $api_url = $this->fix_url($this->api_url['storage'].$storage_path ); $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header ,) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } $request_array = json_decode(json_encode($api_call['data'])); //convert to php array for data parsing $zone_data = json_decode(($api_call['data'])); //--->get all the hostnames > start $files = array(); $folders = array(); //--->get all the hostnames > start if($zone_data) { foreach ($zone_data as $key => $v1) { $folder_path = str_replace('/'.$v1->StorageZoneName.'/',"/",$v1->Path); if(!$v1->IsDirectory) { //files only $d = array ( "storage_zone_name"=>$v1->StorageZoneName, "folder_path"=>$folder_path, "file_name" =>$v1->ObjectName, "file_zone_path" =>$v1->Path.$v1->ObjectName, "file_dl_path" => $folder_path.$v1->ObjectName, ); array_push($files, $d); } else if($v1->IsDirectory) { //folders only $d = array ( "storage_zone_name"=>$v1->StorageZoneName, "main_folder"=>$v1->Path, "sub_folder" =>$v1->ObjectName, "folder_path" =>$v1->Path.$v1->ObjectName, ); array_push($folders, $d); } } } //--->get all the hostnames > end return array( 'status' => 'success', 'zone_smry'=> array('folders' => $folders,'files' => $files,), "zone_details" => json_decode($request_array), ); die(); } public function PutFile($local_upload_file_path ='', $storage_zone_path='', $storage_zone_file_path='' ) { /* will upload a file to storage zone */ if( !$this->api_key_storage) { return array('status' =>'error' ,'code' =>'api_key_storage' ,'msg'=> 'missing storage api key'); die(); } if(!$local_upload_file_path ) { return array('status' =>'error' ,'code' =>'local_upload_file_path' ,'msg'=> 'missing file path'); die(); } if(!$storage_zone_file_path ) { return array('status' =>'error' ,'code' =>'storage_zone_file_path' ,'msg'=> 'missing storage zone file path'); die(); } //file variables //make folder and file name seo friendly to ensure no problem happen $cdn_file_path = $this->seo_file_name($storage_zone_file_path); $path_info = pathinfo($cdn_file_path); //will get folders path $info_dir_name = strtolower($path_info['dirname']); //will get file name with ext $info_file_name = $path_info['basename']; //$info_file_name = $path_info['filename']; $info_file_ext = $path_info['extension']; $storage_file_path = $storage_zone_path .$cdn_file_path; $key = $this->api_key_storage; $api_url = $this->fix_url($this->api_url['storage'].$storage_file_path); $get_header = $this->create_header($key); // Open the file $file = $local_upload_file_path; $fileStream = fopen($file, "r") or die("Unable to open file!"); $dataLength = filesize($file); // print_r($dataLength); // die; // Initialize and configure curl $curl = curl_init(); curl_setopt_array( $curl, array( CURLOPT_CUSTOMREQUEST => 'PUT' , CURLOPT_URL => $api_url , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed , CURLOPT_TIMEOUT => 60000 // in case you are uploading a really BIG file!! , CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security , CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security , CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security , CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security , CURLOPT_INFILE => $fileStream , CURLOPT_INFILESIZE => $dataLength , CURLOPT_UPLOAD => 1 , CURLOPT_HTTPHEADER => array( 'AccessKey: ' . $key ) ) ); // Send the request $response = curl_exec($curl); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); // Cleanup curl_close($curl); fclose($fileStream); if($http_code !=201 ) { //error message $request_array = json_decode(json_encode($response)); $result = array ( "status" => 'error', "http_code"=>$http_code, "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', 'file_name' => $info_file_name , 'storage_file_path' => $storage_file_path, 'cdn_file_path' => $cdn_file_path, 'msg'=> $response, ); die(); } public function GetFile($storage_path ='' ) { /* will get a file from the storage zone */ if(!$storage_path || !$this->api_key_storage) { return array('status' =>'error' ,'code' =>'missing_api_key_storage' ,'msg'=> 'missing storage missing api'); die(); } $key = $this->api_key_storage; $api_url = $this->fix_url($this->api_url['storage'].$storage_path ); $accessKey = $this->api_key_storage; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header ) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } $path_info = pathinfo($storage_path); $file_name = $path_info['basename']; $file = $api_call['data']; header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$file_name"); //will force to download... echo $file ; } public function DeleteFile($storage_path ='') { /* will delete a file from the storage zone */ if(!$storage_path || !$this->api_key_storage) { return array('status' =>'error' ,'code' =>'missing_api_key_storage' ,'msg'=> 'missing storage missing api'); die(); } $key = $this->api_key_storage; $api_url = $this->fix_url($this->api_url['storage'].$storage_path ); $accessKey = $this->api_key_storage; $get_header = $this->create_header($key); $api_call = $this->run( array('call_method' => 'DELETE', 'api_url' => $api_url,'header' => $get_header ) ); if($api_call['http_code'] !=200 ) { //error message $request_array = json_decode(json_encode($api_call['data'])); $result = array ( "status" => 'error', "http_code"=>$api_call['http_code'], "msg" => json_decode($request_array) , ); return $result; die(); } return array( 'status' => 'success', 'msg'=> $api_call, ); die(); } public function SecureLink($host_name ='', $security_key ='', $file_path='', $expiry_hr = 24) { $securityKey = $security_key; $path = $file_path; // Set the time of expiry to one hour from now $expires = (time() + 3600 ) * $expiry_hr; // Generate the token $hashableBase = $securityKey.$path.$expires; // If using IP validation // $hashableBase .= "146.14.19.7"; $token = md5($hashableBase, true); $token = base64_encode($token); $token = strtr($token, '+/', '-_'); $token = str_replace('=', '', $token); // Generate the URL $url = "$host_name$file_path?token={$token}&expires={$expires}" ; return $url; } //--->storage > end public function DownloadFile($file_url = '', $oupt_file_name='') { //this is a fast way to download a file //remove any query string data if(isset($oupt_file_name)) { $file_name = $oupt_file_name; } if(empty($oupt_file_name)) { $file_name = preg_replace('/\?.*/', '', basename($file_url)); } header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=$file_name"); readfile($file_url); } public function DownloadFile1($file_url) { /* this is a slow way to download a file will allow you to download a remote file from any server that is accessible */ $filename = $file_url; $filedata = @file_get_contents($filename); // SUCCESS if ($filedata) { // GET A NAME FOR THE FILE //remove any query string data $basename = preg_replace('/\?.*/', '', basename($file_url)); //$basename = basename($filename); // THESE HEADERS ARE USED ON ALL BROWSERS header("Content-Type: application-x/force-download"); header("Content-Disposition: attachment; filename=$basename"); header("Content-length: " . (string)(strlen($filedata))); header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // THIS HEADER MUST BE OMITTED FOR IE 6+ if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE ')) { header("Cache-Control: no-cache, must-revalidate"); } // THIS IS THE LAST HEADER header("Pragma: no-cache"); // FLUSH THE HEADERS TO THE BROWSER flush(); // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END ob_start(); echo $filedata; } // FAILURE else { die("ERROR: UNABLE TO OPEN $filename"); } } //--->process functions > start private function create_header($api_key) { $header = array('Content-Type:application/json','accesskey:'.$api_key.'' ); return $header; } private function run($call_arr = array('call_method' => 'GET', 'api_url' => 'api_url','header' => array(),'post_data_array' => array() , ) ) { $call_method = isset($call_arr['call_method']) ? $call_arr['call_method'] : 'GET' ; $api_url = isset($call_arr['api_url']) ? $call_arr['api_url'] : 'api_url' ; $header = isset($call_arr['header']) ? $call_arr['header'] : '' ; $post_data_array = isset($call_arr['post_data_array']) ? $call_arr['post_data_array'] : '' ; $post_data = json_encode($post_data_array); $curl = curl_init($api_url); curl_setopt($curl, CURLOPT_HTTPHEADER,$header); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $call_method); curl_setopt($curl, CURLOPT_URL, $api_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($curl); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); //For error checking if ( $result === false ) { return array('status' =>'error' ,'code'=> 'curl_error', 'result' => curl_error($curl) ,); die(); } return array('http_code'=> $http_code, 'data' => $result,); } //--->process functions > end //--->private functions > start private function fix_url($url ='') { return str_replace("\\", "/", $url ); } private function format_bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE) { // Format string $format = ($format === NULL) ? '%01.2f %s' : (string) $format; // IEC prefixes (binary) if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE) { $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); $mod = 1024; } // SI prefixes (decimal) else { $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB'); $mod = 1000; } // Determine unit to use if (($power = array_search((string) $force_unit, $units)) === FALSE) { $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0; } return sprintf($format, $bytes / pow($mod, $power), $units[$power]); } private function seo_file_name($file_name) { /* will convert file name into seo url file name i.e. $file_name = 'code with mark !@#$%^*()_+~ $$%& _03e05 122-9****.mp4'; //output will be code-with-mark-03e05-122-9.mp4 Note only use this for file names and not for folder names!!! */ $path_info = pathinfo($file_name); $info_dir_name = preg_replace("/[\s]/", "-", strtolower($path_info['dirname']) ); $info_file_name = $path_info['filename']; $info_file_ext = $path_info['extension']; $string = $info_file_name ; $src = 'àáâãäçèéêëìíîïñòóôõöøùúûüýÿßÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ'; $rep = 'aaaaaceeeeiiiinoooooouuuuyysAAAAACEEEEIIIINOOOOOOUUUUY'; // strip off accents (assuming utf8 PHP - note strtr() requires single-byte) $string = strtr(utf8_decode($string), utf8_decode($src), $rep); // convert to lower case $string = strtolower($string); // strip all but alphanumeric, whitespace, dot, underscore, hyphen $string = preg_replace("/[^a-z0-9\s._-]/", "", $string); // merge multiple consecutive whitespaces, dots, underscores, hyphens $string = preg_replace("/[\s._-]+/", " ", $string); // convert whitespaces to hyphens $string = preg_replace("/[\s]/", "-", $string); if(substr($info_dir_name,1)) { $file_path = $info_dir_name."/".$string.'.'.$info_file_ext; } else { $file_path = "/". $string.'.'.$info_file_ext; } return $file_path; } //--->private functions > end } |
Also, functions are available
Create New Zone(Website)
1 2 3 4 5 6 7 8 |
$zone_name = 'MyBunnyCDNSite'; $zone_url = 'http://mybunnycdnsite.com'; $d1 = $cdn->Account($account_key)->CreateNewZone($zone_name , $zone_url ); |
Get Single Zone
1 2 3 4 5 6 |
$zoneid = 35617; $d1 = $cdn->Account($account_key)->GetZone($zoneid); |
Get Zone List
1 2 3 4 5 |
$d1 = $cdn->Account($account_key)->GetZoneList(); |
Purge Zone Cache
1 2 3 4 5 6 |
$zoneid = 35617; $d1 = $cdn->Account($account_key)->PurgeZoneCache($zoneid); |
Add Host Name
1 2 3 4 5 6 7 |
$zoneid = 35617; $host_name_url = 'cd.mybunnycdnsite.com'; $d1 = $cdn->Account($account_key)->AddHostName($zoneid,$host_name_url); |
Delete Host Name
1 2 3 4 5 6 7 |
$zoneid = 35617; $host_name_url = 'cd.mybunnycdnsite.com'; $d1 = $cdn->Account($account_key)->DeleteHostName($zoneid,$host_name_url); |
Add Blocked IP
1 2 3 4 5 6 7 8 |
//add host name $zoneid = 35617; $blocked_ip = '153.208.102.234'; $d1 = $cdn->Account($account_key)->AddBlockedIP($zoneid,$host_name_url); |
Remove Blocked IP
1 2 3 4 5 6 7 |
$zoneid = 35617; $blocked_ip = '153.208.102.234'; $d1 = $cdn->Account($account_key)->RemoveBlockedIP($zoneid,$host_name_url); |
Stats
1 2 3 4 5 |
$d1 = $cdn->Account($account_key)->Stats() |
Billing
1 2 3 4 5 |
$d1 = $cdn->Account($account_key)->Billing(); |
Apply Code
1 2 3 4 5 6 |
$promo_code = 'mycode'; $d1 = $cdn->Account($account_key)->ApplyCode($promo_code); |
Delete Single Zone
1 2 3 4 5 6 7 |
//delete a single zone $zoneid = 35617; $d1 = $cdn->Account($account_key)->DeleteZone($zoneid); |
Storage Functions
1 2 3 4 5 6 7 8 9 10 |
//PutFile - api key(or might be labeled FTP "password") $local_upload_file_path = 'file/video1.mp4'; $storage_zone_path = '/mybunnycdnsite'; $storage_zone_file_path = '/my test file-'.uniqid().'.mp4'; $d1 = $cdn->Storage($storage_key)->PutFile($local_upload_file_path, $storage_zone_path, $storage_zone_file_path); |
Put File Into Subfolder Of Storage Zone
1 2 3 4 5 6 7 8 9 10 |
//PutFile $local_upload_file_path = 'file/video1.mp4'; $storage_zone_path = '/mybunnycdnsite'; $storage_zone_file_path = '/my-assets/my test file-'.uniqid().'.mp4'; $d1 = $cdn->Storage($storage_key)->PutFile($local_upload_file_path, $storage_zone_path, $storage_zone_file_path); |
Delete File From Storage Zone
1 2 3 4 5 6 7 |
//DeleteFile $storage_path = '/mybunnycdnsite/my-test-file-5b1994db6d265.mp4'; $d1 = $cdn->Storage($storage_key)->DeleteFile($storage_path); |
More Stories
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu
Text to speech