我正在尝试创建一个简单的小部件来检索用户提要,我已经完全设置了我的应用程序,以便与Twitter的API的OAuth功能@anywhere一起使用。
我遇到的问题是当我尝试检索用户的推文并将它们再次写出到文本文件中时。我这样做的代码如下
<?php
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/statuses/user_timeline/screen_name.json?count=3&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
好的,现在当我在本地运行应用程序时,这段代码效果很好,但是当我将其部署到服务器时,它不起作用。它在服务器上创建缓存文件和我添加的任何测试数据,以检查文件写入过程是否有效。
我没有自己设置服务器,它由外部公司运行,我只是使用 ftp 命令来部署 Web 应用程序
任何帮助和想法将不胜感激
请咨询您的主机 - 出于安全原因,他们几乎肯定不允许file_get_contents。
您的选择是
- 要求他们通过编辑 php.ini 来启用它allow_url_fopen
- 使用 cURL 进行调用
这是在 php 中使用 curl 的基本示例
$url = "http://api.twitter.com/1/statuses/user_timeline/screen_name.json?count=3&include_rts=true&include_entities=true"
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_URL,$url);
$data = curl_exec($curl_handle);
curl_close($curl_handle);