在命令列下執行php程式

來源:互聯網
上載者:User
問題

安裝phpredis擴充,在PHP.ini中添加extension=redis.so後,編寫Redis.php檔案

<code class="language-php hljs  has-numbering"><span class="hljs-comment">// Redis.php</span><span class="hljs-preprocessor"><?php</span>    <span class="hljs-variable">$client</span> = <span class="hljs-keyword">new</span> Redis();    <span class="hljs-variable">$client</span>->set(<span class="hljs-string">'key'</span>, <span class="hljs-string">'value'</span>);    <span class="hljs-keyword">echo</span> <span class="hljs-variable">$client</span>->get(<span class="hljs-string">'key'</span>);    ....</code><ul style="opacity: 0.0587674;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>

使用php命令列執行Redis.php,但是確遇見一下問題

<code class="language-php hljs  has-numbering">$ php Redis.php$ ....<span class="hljs-string">" Fatal error: Class 'Redis' not found in Redis.php"</span>...</code><ul style="opacity: 0.0587674;" class="pre-numbering"><li>1</li><li>2</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>

但是,在瀏覽器中可以顯示echo $client->get('key');的值,這是為什麼。 解決過程 網上搜尋

網上尋找Fatal error: Class 'Redis' not found in Redis.php關鍵詞。

終於在stackoverflow找到相似提問:phpredis errors Class Redis not found in Linux

僅有的一個回複,命令列和web server可能使用不同的php.ini檔案
php命令列和web Server使用相同的php.ini嗎。

<code class="language-php hljs  has-numbering"><span class="hljs-comment">// 去找php的doc</span>$ man php<span class="hljs-comment">// 終於找到相關資訊</span>...FILES    /etc/php5/cli/php.ini        The configuration file <span class="hljs-keyword">for</span> the CLI version of PHP    /etc/php5/cgi/php.ini.        The configuration file <span class="hljs-keyword">for</span> the CGI version of PHP    /etc/php5/apache2/php.ini.        The configuration file <span class="hljs-keyword">for</span> the version of PHP that apache2 uses.        ...</code><ul style="opacity: 0.0587674;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li></ul>
解決方案

修改的php.ini準確的說是/etc/php5/apache2/php.ini,因此僅web server可以使用phpredis擴充。
在/etc/php5/cli/php.ini中添加extension=redis.so後,執行$ php Redis.php,測試成功。


來源:http://blog.csdn.net/renwotao2009/article/details/46981037

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.