How to load Zend Framework into PHP files


Zend Framework provide a lot of interesting libraries. Instead re-invent the wheel, using Zend Framework libraries will boost our development time. You can download latest ZendFramework on http://framework.zend.com/download/latest . To use Zend Framework with our system like individual php files, CMS or Framework, we should configure our path.

This is scheme of folders and extracted Zend Framework:

1
2
3
Project Folder
   |__ Zend
   |__ my.php


Our purpose is to load Zend framework into my.php. Edit my.php :

1
2
3
4
5
6
7
8
9
10
11
12
<?php
/* Load Zend Framework */
// include path for Zend Framework
// alter it accordingly if you have put the ‘Zend’ folder elsewhere
ini_set(‘include_path’,
ini_get(‘include_path’) . PATH_SEPARATOR . ‘Zend’);

// now include path is setup and we can use zend
require_once ‘Zend/Loader/Autoloader.php’;
Zend_Loader_Autoloader::getInstance();

?>

Now we already use Zend AutoLoader and we can create instance as we need. For example, i want to use Zend Json Decode to parse twitter search json.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/* Load Zend Framework */
// include path for Zend Framework
// alter it accordingly if you have put the ‘Zend’ folder elsewhere
ini_set(‘include_path’,
ini_get(‘include_path’) . PATH_SEPARATOR . ‘Zend’);

// now include path is setup and we can use zend
require_once ‘Zend/Loader/Autoloader.php’;
Zend_Loader_Autoloader::getInstance();

$url = "http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed";
$tfile = @file_get_contents($url);
$data  = Zend_Json::decode($tfile);
var_dump($data);

?>

And you will got result :

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
array(11) {
  ["completed_in"]=>
  float(0.05)
  ["max_id"]=>
  int(134919423252373504)
  ["max_id_str"]=>
  string(18) "134919423252373504"
  ["next_page"]=>
  string(74) "?page=2&max_id=134919423252373504&q=blue%20angels&rpp=5&include_entities=1"
  ["page"]=>
  int(1)
  ["query"]=>
  string(11) "blue+angels"
  ["refresh_url"]=>
  string(63) "?since_id=134919423252373504&q=blue%20angels&include_entities=1"
  ["results"]=>
  array(5) {
    [0]=>
    array(15) {
      ["created_at"]=>
      string(31) "Fri, 11 Nov 2011 09:04:31 +0000"
      ["entities"]=>
      array(1) {
        ["urls"]=>
        array(1) {
          [0]=>
          array(4) {
            ["url"]=>
            string(20) "http://t.co/DbaLGEiA"
            ["expanded_url"]=>
            string(21) "http://amzn.to/vfsQvi"
            ["display_url"]=>
            string(14) "amzn.to/vfsQvi"
            ["indices"]=>
            array(2) {
              [0]=>
              int(116)
              [1]=>
              int(136)
            }
          }
        }
      }
      ["from_user"]=>
      string(7) "Godarup"
      ["from_user_id"]=>
      int(395388097)
      ["from_user_id_str"]=>
      string(9) "395388097"
      ["geo"]=>
      NULL
      ["id"]=>
      int(134919423252373504)
      ["id_str"]=>
      string(18) "134919423252373504"
      ["iso_language_code"]=>
      string(2) "en"
      ["metadata"]=>
      array(1) {
        ["result_type"]=>
        string(6) "recent"
      }
      ["profile_image_url"]=>
      string(65) "http://a1.twimg.com/profile_images/1599593594/MFC-2634_normal.jpg"
      ["source"]=>
      string(95) "&lt;a href=&quot;http://twitterfeed.com&quot; rel=&quot;nofollow&quot;&gt;twitterfeed&lt;/a&gt;"
      ["text"]=>
      string(136) "Blue Angels Wallpaper Border: Angels Cherubs laser-cut wallpaper border.The background is in shades of blue and … http://t.co/DbaLGEiA"
      ["to_user_id"]=>
      NULL
      ["to_user_id_str"]=>
      NULL
    }
    [1]=>
    array(15) {
      ["created_at"]=>
      string(31) "Fri, 11 Nov 2011 08:47:02 +0000"
      ["entities"]=>
      array(1) {
        ["urls"]=>
        array(1) {
          [0]=>
          array(4) {
            ["url"]=>
            string(20) "http://t.co/CCM0DtJD"
            ["expanded_url"]=>
            string(20) "http://bit.ly/vTWGhg"
            ["display_url"]=>
            string(13) "bit.ly/vTWGhg"
            ["indices"]=>
            array(2) {
              [0]=>
              int(52)
              [1]=>
              int(72)
            }
          }
        }
      }
      ["from_user"]=>
      string(6) "VeldaM"
      ["from_user_id"]=>
      int(18832271)
      ["from_user_id_str"]=>
      string(8) "18832271"
      ["geo"]=>
      NULL
      ["id"]=>
      int(134915020315299840)
      ["id_str"]=>
      string(18) "134915020315299840"
      ["iso_language_code"]=>
      string(2) "en"
      ["metadata"]=>
      array(1) {
        ["result_type"]=>
        string(6) "recent"
      }
      ["profile_image_url"]=>
      string(68) "http://a3.twimg.com/profile_images/70488171/velda-square2_normal.JPG"
      ["source"]=>
      string(95) "&lt;a href=&quot;http://twitterfeed.com&quot; rel=&quot;nofollow&quot;&gt;twitterfeed&lt;/a&gt;"
      ["text"]=>
      string(72) "blue angels coming to pensacola for homecoming show http://t.co/CCM0DtJD"
      ["to_user_id"]=>
      NULL
      ["to_user_id_str"]=>
      NULL
    }
    [2]=>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.