使用PHP连接到Twitpic API

时间:2011-03-08 07:21:38

标签: php api twitpic

我正在为应用程序使用Twitpic API。我有点卡住,因为我似乎无法找到错误。这段代码我读过的所有文档都说它是正确的。帮助将不胜感激。谢谢!

function do_twitpic() 
{           
    $media = 'http://image-to-upload.jpg'; 
    $username = $_POST['username'];
    $password = $_POST['password'];

    $postfields = array(); 
    $postfields['username'] = $username; 
    $postfields['password'] = $password;  
    $postfields['media'] = "@".$media;

    $twitter_url = 'http://twitpic.com/api/upload'; 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_URL, $twitter_url); 
    curl_setopt($curl, CURLOPT_POST, 3); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields); 
    $result = curl_exec($curl); 
    curl_close($curl); 
    $login_xml = new SimpleXMLElement($result); 
    if (isset($login_xml->error)) { 
        print_r($login_xml); 
    } else { 
        print_r($login_xml); 
    }   
}

非常感谢!

2 个答案:

答案 0 :(得分:0)

根据此页面,$ media需要是图像的二进制编码数据,而不是它的URL。 http://www.twitpic.com/api.do#upload

答案 1 :(得分:0)

图像的“真实路径”前面的@,而不是公共URL,会动态地将图像转换为二进制数据。