Php 文件获取内容到一个json变量中-twitter模块
我对json非常陌生,已经为这个问题工作了大约一周 我使用php从帐户列表中检索推文,并将其存储到.txt文件中Php 文件获取内容到一个json变量中-twitter模块,php,javascript,json,file-upload,twitter,Php,Javascript,Json,File Upload,Twitter,我对json非常陌生,已经为这个问题工作了大约一周 我使用php从帐户列表中检索推文,并将其存储到.txt文件中 $cache = dirname(__FILE__) . '/../cache/twitter-json.txt'; $data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
架构师构建前端页面的方式是,我需要将json值(我在.txt文件中拥有的内容)存储到.js文件中的json变量中,并呈现它
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
编辑:
所以改成了
$cache =_ _DIR__.'/cached.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
file_put_contents($cache, $data);
文件是空的。你们知道有什么问题吗
是否可以将.txt文件的内容存储到.js文件中的json变量中
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
utf8\u编码,因为JSON已经是UTF-8了
文件内容
file_put_内容($cache,'var myvar=.$data.;)代码>
阐明我的解决方案的代码:
$cache = __DIR__.'/cached.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json?slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
file_put_contents($cache, 'var mydata = '.$data.';');
utf8\u编码,因为JSON已经是UTF-8了
文件内容
file_put_内容($cache,'var myvar=.$data.;)代码>
阐明我的解决方案的代码:
$cache = __DIR__.'/cached.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json?slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
file_put_contents($cache, 'var mydata = '.$data.';');
是否可以将.txt文件的内容存储到.js文件中的json变量中
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
是的,有可能:
$txtFile = '/path/to/txt-file.txt';
$jsFile = '/path/to/js-file.js';
$jsPlate = "var jsVariable = %s;\n";
$string = file_get_contents($txtFile);
if (FALSE === $string) {
throw new RuntimeException('Failed to load text-file.');
}
$json = json_encode($string);
if (FALSE === $json) {
throw new RuntimeException('Failed to json encode string.');
}
$jsString = sprintf($jsPlate, $json);
$result = file_put_contents($jsFile, $jsString);
if (!$result) {
throw new RuntimeException('Failed to save javascript-file.');
}
编码检查表:
- 文本文件是UTF-8编码的
$cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
$data = file_get_contents('http://api.twitter.com/1/lists/statuses.json? slug=widget&owner_screen_name=webcodepro&count=1&page=1&per_page=8&include_rts=true&include_entities=true');
$cachefile = fopen($cache, 'wb');
fwrite($cachefile,utf8_encode($data));
fclose($cachefile);
?>
是的,有可能:
$txtFile = '/path/to/txt-file.txt';
$jsFile = '/path/to/js-file.js';
$jsPlate = "var jsVariable = %s;\n";
$string = file_get_contents($txtFile);
if (FALSE === $string) {
throw new RuntimeException('Failed to load text-file.');
}
$json = json_encode($string);
if (FALSE === $json) {
throw new RuntimeException('Failed to json encode string.');
}
$jsString = sprintf($jsPlate, $json);
$result = file_put_contents($jsFile, $jsString);
if (!$result) {
throw new RuntimeException('Failed to save javascript-file.');
}
编码检查表:
- 文本文件是UTF-8编码的
utf8\u编码,因为JSON已经是UTF-8了。utf8\u编码($data)为什么要utf8\u编码数据?我敢打赌,我的第一个孩子,你这样做只会制造问题。@deceze也注意到了这一点,但它是完全安全的,因为JSON不以任何方式使用非ASCII字符。@Tom这不是保证,JSON数据格式可以包含UTF-8字符。PHP输出的JSON并不是这样。@deceze-Twitter只保证ASCII JSON。但是你是对的——JSON可以有UTF-8字符。当然,仍然不需要utf8\u编码
,因为JSON已经是UTF-8了。utf8\u编码($data)可能需要将myvar
添加到全局空间。@Florent当您将其作为
添加时,您可以从其他脚本以myvar
的形式访问它。噢,非常感谢各位。原谅我,我对这件事很陌生。那么我可以做$cache=cached.js吗?是的,Christina,$cache只是您要写入的文件的位置。请注意,如果$cache='cached.js',cached.js必须与正在运行的根PHP脚本位于同一目录中。我建议您不要尝试这样做。但是,您可以将JSON解码为数组JSON\u decode($data,true)
位确保它是数组。然后使用array\u merge()
或自定义函数合并两个数组,最后使用json\u encode($merged\u array)
创建新的json对象。现在只需将其写入文件。您可能需要将myvar
添加到全局空间。@Florent当您将其添加为
时,您只需从其他脚本以myvar
的形式访问它。噢,太棒了,谢谢大家。原谅我,我对这件事很陌生。那么我可以做$cache=cached.js吗?是的,Christina,$cache只是您要写入的文件的位置。请注意,如果$cache='cached.js',cached.js必须与正在运行的根PHP脚本位于同一目录中。我建议您不要尝试这样做。但是,您可以将JSON解码为数组JSON\u decode($data,true)
位确保它是数组。然后使用array\u merge()
或自定义函数合并两个数组,最后使用json\u encode($merged\u array)
创建新的json对象。现在只需将其写入文件。谢谢@hakre,但奇怪的是,我的.js文件仍然是空的:-(它保存在同一个目录中,因此写为$jsFile=“/cached.js”你知道这可能是什么原因吗?我写的路径不正确吗?/cached.js
不在同一个目录中。请尝试以下操作:$jsFile=dirname(\uu文件\uuu)“/cached.js”;
。它与php代码所在的script.php
位于同一目录中。谢谢@hakre,但奇怪的是,我的.js文件仍然是空的:-(它保存在同一目录中,因此写为$jsFile=“/cached.js”你知道这可能是什么原因吗?我写的路径不正确吗?/cached.js
不在同一个目录中。请尝试以下操作:$jsFile=dirname(\uuu FILE\uu)。“/cached.js”;
。它与php代码所在的脚本.php
位于同一目录中。