【发布时间】:2012-03-17 16:13:20
【问题描述】:
我对 twitter api 非常陌生,今天是我人生中第一次注册 twitter api。我的目标是在推特上发布我网站上的图片和一条消息。我已经从 Internet 下载了它当前的库,它给了我以下 2 个文件和一些示例。
我已经包含了这两个文件并得到了这个代码 photo_tweet.php 表示带有照片的推文
这是我的代码
<?php
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'ZlhOWeCWG2MS5Wxxxxxx',
'consumer_secret' => 'DIPjoKcIWjpGWmw5jGJSKGAOLxxxxxx',
'user_token' => 'xxxxx-OGtPyRSUOUaR6XQRLAFVuv14xxxxxx',
'user_secret' => 'xxxxxoimsrNFhlz7mPa9h5pyVSjxxxxxxx',
));
// we're using a hardcoded image path here. You can easily replace this with
// an uploaded image - see images.php in the examples folder for how to do this
// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
// this is the jpeg file to upload. It should be in the same directory as this file.
$image = 'a.jpg';
$code = $tmhOAuth->request(
'POST',
'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$image};type=image/jpeg;filename={$image}",
'status' => 'Picture time',
),
true, // use auth
true // multipart
);
if ($code == 200) {
tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
} else {
tmhUtilities::pr($tmhOAuth->response['response']);
}
?>
a.jpg 在我的服务器上。
这是我在推特上的设置!
请求类型:GET 请求 URI:* https://api.twitter.com/1/
当我上传所有这些代码并访问我的代码时,它返回空白屏幕,当我回显 $code;它返回 0
这就是我所观察和经历的一切。我需要你的帮助。我会非常非常感谢你
注意:事件当我删除我的消费者密钥和消费者密码时,此代码不会显示任何错误。只是一个空白屏幕。我希望它在出现任何问题但没有错误时显示错误
这是状态代码,只对我有用
$consumerKey = $consumer_key;
$consumerSecret = $consumer_secret;
$OAuthToken = $user_token;
$OAuthSecret = $user_secret;
include "OAuth.php";
include "twitteroauth.php";
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);
$image = 'a.jpg';
$optsArray['status'] = 'Hi';
if (isset($_GET['msg'])) {
$tweetmsg = $_GET['msg'];
$tweet->post('statuses/update',$optsArray);
if($tweet)
echo "Your message has been sent to Twitter.";
else
echo "Your message has not been sent to Twitter.";
} else {
echo "Your message has not been sent to Twitter.";
}
但它只在我使用时发布状态
update_with_media.json
和
$optsArray['@media[]'] = "@{$image}";
它不起作用,请帮我解决这个问题
【问题讨论】:
-
你试过推特发简单的信息吗?我建议您先发布一条简单的消息来测试您的代码/api 密钥/令牌
-
我使用另一个简单的代码进行测试,但它也给出了空白屏幕。我已经从 twitter 设置中正确复制了我的密钥和令牌。
标签: php json twitter jsonp twitter-oauth