curl failed to upload image to api

WBOY
Release: 2016-08-18 09:16:35
Original
1036 people have browsed it

<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" /》
    <input type="submit" value="Submit" />
    </form>';
?>
</code>
Copy after login
Copy after login

There is no error message on the page, and when I open Weibo’s API, it shows a 405 error. I don’t know if it’s the API itself.

Reply content:

<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" /》
    <input type="submit" value="Submit" />
    </form>';
?>
</code>
Copy after login
Copy after login

There is no error message on the page, and when I open Weibo’s API, it shows a 405 error. I don’t know if it’s the API itself.

If the version of php is 5.5 or above
You cannot use @+file path when uploading files with curl.
Use CURLFile instead, even curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); is useless

Set up the protocol and give it a try! header("Content-Type:multipart/form-data")

Related labels:
php
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