Home > Backend Development > PHP Tutorial > oauth2: 对 api 如何上传文件?

oauth2: 对 api 如何上传文件?

WBOY
Release: 2016-06-06 20:38:29
Original
1093 people have browsed it

环境是:

  • yii2-authclient
  • weibo.upload接口

感谢思路/点拨, 这个问题应该不限定在yii2框架内.

接口文档:
pic 类型: binary 描述: 要上传的图片,仅支持JPEG、GIF、PNG格式,图片大小小于5M。

我的代码:

<code>public function sendUpload($text, $url)
{
    return $this->api('2/statuses/upload.json', 'POST', [
        'status' => $text,
        'visible' => self::VISIBLE_ALL,
        'pic' => '@'.$url,
        'source' => $this->source,
    ]);
}
</code>
Copy after login
Copy after login

执行后返回的错误:

<code>{"error":"unsupport mediatype (application/x-www-form-urlencoded)","error_code":10007,"request":"/2/statuses/upload.json"}
</code>
Copy after login
Copy after login

微博提供的演示代码:

<code>function upload( $status, $pic_path, $lat = NULL, $long = NULL )
{
    $params = array();
    $params['status'] = $status;
    $params['pic'] = '@'.$pic_path;
    if ($lat) {
        $params['lat'] = floatval($lat);
    }
    if ($long) {
        $params['long'] = floatval($long);
    }

    return $this->oauth->post( 'statuses/upload', $params, true );
}
</code>
Copy after login
Copy after login

回复内容:

环境是:

  • yii2-authclient
  • weibo.upload接口

感谢思路/点拨, 这个问题应该不限定在yii2框架内.

接口文档:
pic 类型: binary 描述: 要上传的图片,仅支持JPEG、GIF、PNG格式,图片大小小于5M。

我的代码:

<code>public function sendUpload($text, $url)
{
    return $this->api('2/statuses/upload.json', 'POST', [
        'status' => $text,
        'visible' => self::VISIBLE_ALL,
        'pic' => '@'.$url,
        'source' => $this->source,
    ]);
}
</code>
Copy after login
Copy after login

执行后返回的错误:

<code>{"error":"unsupport mediatype (application/x-www-form-urlencoded)","error_code":10007,"request":"/2/statuses/upload.json"}
</code>
Copy after login
Copy after login

微博提供的演示代码:

<code>function upload( $status, $pic_path, $lat = NULL, $long = NULL )
{
    $params = array();
    $params['status'] = $status;
    $params['pic'] = '@'.$pic_path;
    if ($lat) {
        $params['lat'] = floatval($lat);
    }
    if ($long) {
        $params['long'] = floatval($long);
    }

    return $this->oauth->post( 'statuses/upload', $params, true );
}
</code>
Copy after login
Copy after login

你需要 multipart/form-data.

具体参考这个 gist

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template