检索用户的推文并插入db

时间:2012-04-01 11:57:07

标签: php sql twitter oauth echo

我使用以下代码将图片上传到我的rackspace cdn帐户。上传工作正常。假设,我从tweetbot发了一张照片,它会自动上传到我的rackspace cdn。我想要做的是,检索我推文的消息,并将消息和照片链接插入数据库。

这是我到目前为止所做的......

$oauthecho = new TwitterOAuthEcho();
$oauthecho->userAgent = 'dpkgme test app';
$oauthecho->setCredentialsFromRequestHeaders();

if ($oauthecho->verify()) {      

 // Verification was a success, we should be able to access the user's Twitter info    from the responseText.
 $userInfo = json_decode($oauthecho->responseText, true);
 $twitterId = isset($userInfo['id']) ? $userInfo['id'] : null;
 $tweet = isset($userInfo['message']) ? $userInfo['message'] : null;

                    error_reporting(E_ALL);
                    ini_set('display_errors', 1);

                    // include the API
                    require("cloudfiles.php") ;

                    // cloud info
                    $username = 'username'; // username
                    $key ='apikey' ; // api key
                    $container = 'container'; // container name

                    ob_start();


                    $localfile = $_FILES['media']['tmp_name'];
                    $filename = $_FILES['media']['name'];
                    $nf = time().'-'.$filename; 


                    ob_flush();

                    // Connect to Rackspace
                    $auth = new CF_Authentication($username, $key);
                    $auth->authenticate();
                    $conn = new CF_Connection($auth);

                    // Get the container we want to use
                    $container = $conn->create_container($container);

                    // store file information

                    ob_flush();

                    // upload file to Rackspace
                    $object = $container->create_object($nf);
                    $object->load_from_filename($localfile);

                    $uri = $container->make_public();
                    //print "Your URL is: " . $object->public_uri();

                    $imagePageUrl = $object->public_uri();


                    ob_end_flush();


                    echo '<mediaurl>http://url/'.$nf.'</mediaurl>';

                    $link = 'http://url/'.$nf ; 


                    $con = mysql_connect("localhost","username","password");
                    if (!$con)
                         {
                             die('Could not connect: ' . mysql_error());
                         }

                    mysql_select_db("database", $con);

                    mysql_query("INSERT INTO photo (link)VALUES ('$link','$tweet')");


                    mysql_close($con);

 } else {
   // verification failed, we should return the error back to the consumer
   $response = json_decode($oauthecho->responseText, true);      
   $message = isset($response['error']) ? $response['error'] : null;      
    if (!headers_sent())
    header('HTTP/1.0 ' . $oauthecho->resultHttpCode);
   echo $message;
  } 

我上传了一些照片。上传工作正常,网址返回tweetbot。但是,没有任何内容会附加到我的数据库请帮忙

1 个答案:

答案 0 :(得分:0)

您尝试插入2个字段,但只声明一个字段名称:

INSERT INTO photo (link)VALUES ('$link','$tweet')

你需要做类似的事情:

INSERT INTO photo (link, tweet) VALUES ('$link','$tweet')

您可以在下次使用mysql_error查看此内容