使用Twitter API发布图片不发布

时间:2015-08-13 20:39:51

标签: php html twitter

我正在尝试创建一个简单的应用,在提交表单时发送带有标题的图像的推文。该测试似乎在https://visualstudiogallery.msdn.microsoft.com/5d345edc-2e2d-4a9c-b73b-d53956dc458d工作,但推文从未发布。

为什么提交的推文没有发布?

这是表格......

<form method="post" action="<?php echo bloginfo('template_directory');?>/tweet/start.php" enctype="multipart/form-data" onsubmit="FSV.initTweetValidate()" >
<div id="tweetWrap" class="clearfix">
    <div id="imageDrop">
        <span class="desc">Upload Image</span>
        <input type="file" name="img" id="img"/>
    </div>
    <div id="tweetText">
        <textarea type="text" name="txt" id="txt" maxlength="140" onkeyup="FSV.initCountChar(this)" placeholder="@5StarVintage..."></textarea>
        <div id="charNum">140</div>
    </div>
</div>
<input type="submit" name="sub" class="tweet" id="sub" value="Submit"/>
</form>

这是表单操作代码,start.php ..

<?php

require './config.php';
require './tmhOAuth.php';

/////// upload the photo
$img =  $_FILES["img"]["name"]; 
move_uploaded_file($_FILES["img"]["tmp_name"],$img);

////////// generate *temp* access token and save it in cookie for callback page
$tmhOAuth = new tmhOAuth(array(
    'consumer_key'          => API_KEY,
    'consumer_secret'       => API_SEC, 
    'curl_ssl_verifypeer'   => false
));

$tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''));
$response = $tmhOAuth->extract_params($tmhOAuth->response["response"]);  

$txt = $_POST['txt'];
$temp_token = $response['oauth_token']; 
$temp_secret = $response['oauth_token_secret']; 
$time = $_SERVER['REQUEST_TIME'];

setcookie("Temp_Token", $temp_token, $time + 3600 * 30, '/tweet/');
setcookie("Temp_Secret", $temp_secret, $time + 3600 * 30, '/tweet/'); 
setcookie("Img_Url", $img, $time + 3600 * 30, '/tweet/'); 
setcookie("Tweet_Txt", $txt, $time + 3600 * 30, '/tweet/');

///////// redirect to twitter page for user authincation 
$url = $tmhOAuth->url("oauth/authorize", "") . '?oauth_token=' . $temp_token;
header("Location:".$url);  

// after user give the required authrization he will be redirect to callback.php on your serve
exit();

?>

这是callback.php ..

<?php

require './config.php';
require './tmhOAuth.php';
require './tmhUtilities.php';

/// retrive temp access token from cookie 
$token = $_COOKIE['Temp_Token'];
$secret = $_COOKIE['Temp_Secret'];
$img = $_COOKIE['Img_Url'];
$txt = $_COOKIE['Tweet_Txt'];

$tmhOAuth = new tmhOAuth(array(
    'consumer_key'          => API_KEY,
    'consumer_secret'       => API_SEC,
    'user_token'            => $token,
    'user_secret'           => $secret, 
    'curl_ssl_verifypeer'   => false
));

/// Ask Twitter for correct access token 
$tmhOAuth->request("POST", $tmhOAuth->url("oauth/access_token", ""), array(  
    // pass the oauth_verifier received from Twitter  
    'oauth_verifier' => $_GET["oauth_verifier"]  
)); 

$response = $tmhOAuth->extract_params($tmhOAuth->response["response"]);
$tmhOAuth->config["user_token"] = $response['oauth_token'];  
$tmhOAuth->config["user_secret"] = $response['oauth_token_secret']; 

$img = './'.$img;
$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
array(
    'media[]'  => "@{$img}",
    'status'   => "$txt" // Don't give up..
),
    true, // use auth
    true  // multipart
);

if ($code == 200){
    tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
    echo '<h1>Your image tweet has been sent successfully</h1>';
}else{
    // display the error 
    tmhUtilities::pr($tmhOAuth->response['response']);
    return tmhUtilities;
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Tweet an Image</title>
</head>

<body>
</body>

0 个答案:

没有答案