请求格式来推特图像

时间:2014-02-15 17:34:53

标签: php twitter

我找不到在Twitter上发布更新的正确格式,似乎很明显无人解释。

我正在使用PHP和http://www.neilcrookes.com/2010/04/12/cakephp-oauth-extension-to-httpsocket/

中的http_socket_oauth库

对我来说真正困难的部分是'media []'参数,我不知道确切的位置,以及它应该如何编写。我尝试过很多事情,但总是得到同样的错误:

  

{“errors”:[{“code”:195,“message”:“网址参数丢失或无效。”}]}

这是我认为必须正常的请求,但它不是:

$request = array(
        'method' => 'POST',
        'header' => array(
            'Content-Type' => 'multipart/form-data',
        ),
        'uri' => array(
          'host' => 'api.twitter.com',
          'path' => '1.1/statuses/update_with_media.json',
          'scheme' => 'https',
        ),
        'auth' => array(
          'method' => 'OAuth',
          'oauth_token' => $this->Session->read('Twitter.oauth_token'), // From session
          'oauth_token_secret' => $this->Session->read('Twitter.oauth_token_secret'), // From session
          'oauth_consumer_key' => Configure::read('Twitter.consumer_key'), // APP key
          'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'), // APP secret
        ),
        'body' => array(
          'status' => 'This is a Test ',
          'media[]' => '@{http://www.mydomail.com/img/Drop100.gif},
        ),
      );

谢谢!

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。

问题是来自http://www.neilcrookes.com/2010/04/12/cakephp-oauth-extension-to-httpsocket/的http_socket_oauth lib没有为多部分表单准备数据。所以我在发送请求之前添加了Multipart的句柄。

来自我的控制器的请求以这种方式构建:

        $request = array(
            'method' => 'POST',
            'header' => array(
                'Content-Type' => 'multipart/form-data',
            ),
            'uri' => array(
              'host' => 'api.twitter.com',
              'path' => '1.1/statuses/update_with_media.json',
              'scheme' => 'https',
            ),
            'auth' => array(
              'method' => 'OAuth',
              'oauth_token' => $this->Session->read('Twitter.oauth_token'), // From Session
              'oauth_token_secret' => $this->Session->read('Twitter.oauth_token_secret'), // From Session
              'oauth_consumer_key' => Configure::read('Twitter.consumer_key'),
              'oauth_consumer_secret' => Configure::read('Twitter.consumer_secret'),
            ),
            'data' => array(
                'media[]' => './img'.DS.'Dropial-logo-128.png',
            ),
            'body' => array(
              'status' => 'This is a test!',
            ),
          );

我添加到http_socket_oauth lib的代码是:

if(isset($request['header']['Content-Type']) && $request['header']['Content-Type']=='multipart/form-data'){
App::uses('String', 'Utility');
    $boundary = String::uuid();

    $body = "--{$boundary}\r\n";

    foreach ($request['body'] as $key => $value) {
        $body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
        $body .= "\r\n";
        $body .= "{$value}\r\n";
        $body .= "--{$boundary}\r\n";
    }

    foreach ($request['data'] as $key => $path) {
        $body .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$path}\"\r\n";
        $body .= "\r\n";
        $body .= file_get_contents($path) . "\r\n";
        $body .= "--{$boundary}--\r\n";
    }
    $request['body'] = $body;
    $request['header']['Content-Type'] = "multipart/form-data; boundary={$boundary}";
}

正如你所看到的,我必须重写正文和标题参数。

这就在

之前
return parent::request($request);

就是这样。无论如何,谢谢Terence!