使用PHP OAuth上传Twitter API


Twitter API upload with PHP OAuth

几个小时以来,我一直在努力为Twitter寻找最好的OAuth PHP库。Matt Harris的TmhOAuth看起来有点臃肿(没有冒犯),在我"安装"它之后就开始拍摄PHP警告和严格标准通知。

我只想通过API更新我的bg照片。只是我的,所以不需要任何登录和任何类型的回调,所有的密钥都是硬编码的。

最后,我发现了PHP自己的东西:http://php.net/manual/en/book.oauth.php

看起来很酷,因为所有东西都有4行代码。auth起作用,我可以通过API推送东西,但我似乎无法发送图像参数。这是一种方法:https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image

我发现了一些使用上传表单的例子,但我已经将照片保存在一个文件中,那么我如何提供,quote,base64编码的图像作为原始的多部分数据

$oauth->fetch
(
    'https://api.twitter.com/1/account/update_profile_background_image.json',
    array
    (
        'image' => '@' . $img_path . ';type=image/jpeg'
    ),
    'POST'
);

不起作用,相反我得到

致命错误:未捕获异常"OAuthException",消息为"无效身份验证/错误请求(得到500,预期HTTP/1.1 20X或重定向)"

您可以这样编码图像:

<?php 
$im = imagecreatefromjpeg('file.jpg'); 
$im_Data = base64_encode($im); 
?>

然后,您应该能够将$img_Data添加到api调用中。

我还不知道这是否是一个悬而未决的问题,但"image"键也必须以"@"开头。

$oauth->fetch
(
    'https://api.twitter.com/1/account/update_profile_background_image.json',
    array
    (
        '@image' => '@' . $img_path . ';type=image/jpeg'
    ),
    'POST'
);

如果有人感兴趣,我最终使用了TmhOAuth。膨胀了,但它完成了任务。

$image = array
(
    "@$path",
    'type=image/jpeg',
    "filename=$name"
);
$params = array
(
    'image' => implode(';', $image),
    'use' => 'true'
);
// Request
$code = $tmhOAuth->request('POST', $tmhOAuth->url
(
    '1/account/update_profile_background_image'
),
$params, true, true);