curl向api上传图片失败

WBOY
Lepaskan: 2016-08-18 09:16:35
asal
1037 orang telah melayarinya

<code>   <?php function upload_file($url,$filename,$path,$type){
        $data = array(
            'access_token' => '**************',
            'status' => 'img',
            'visible'=>1,
            'pic'=>'@'.realpath($path).";type=".$type.";filename=".$filename
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return_data = curl_exec($ch);
        curl_close($ch);
        $decode = json_decode($return_data);
        echo $decode->original_pic;       
   }

    if ($_POST) {
        $url = 'https://upload.api.weibo.com/2/statuses/upload.json';
        $path = $_SERVER['DOCUMENT_ROOT'];
        $tmpname = $_FILES['fname']['name'];
        $tmpfile = $_FILES['fname']['tmp_name'];
        $tmpType = $_FILES['fname']['type'];
        upload_file($url,$tmpname,$tmpfile,$tmpType);
        exit;
    }
    echo '<form action="" enctype="multipart/form-data" method="post">
    <input type="file" name="fname" value="Submit">
    </form>';
?>
</code>
Salin selepas log masuk
Salin selepas log masuk

页面没有报错信息,而且我打开微博的api显示405错误,不知道是不是api本身的原因。

回复内容:

<code>   <?php function upload_file($url,$filename,$path,$type){
        $data = array(
            'access_token' => '**************',
            'status' => 'img',
            'visible'=>1,
            'pic'=>'@'.realpath($path).";type=".$type.";filename=".$filename
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return_data = curl_exec($ch);
        curl_close($ch);
        $decode = json_decode($return_data);
        echo $decode->original_pic;       
   }

    if ($_POST) {
        $url = 'https://upload.api.weibo.com/2/statuses/upload.json';
        $path = $_SERVER['DOCUMENT_ROOT'];
        $tmpname = $_FILES['fname']['name'];
        $tmpfile = $_FILES['fname']['tmp_name'];
        $tmpType = $_FILES['fname']['type'];
        upload_file($url,$tmpname,$tmpfile,$tmpType);
        exit;
    }
    echo '<form action="" enctype="multipart/form-data" method="post">
    <input type="file" name="fname" value="Submit">
    </form>';
?>
</code>
Salin selepas log masuk
Salin selepas log masuk

页面没有报错信息,而且我打开微博的api显示405错误,不知道是不是api本身的原因。

如果php的版本是5.5以上
用curl上传文件时不能用@+文件路径的方式了.
要用CURLFile来代替, 就算curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 也没有用

设置一下协议试试看呢!header("Content-Type:multipart/form-data")

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan