post - php curl上传文件如何像表单一样指定其name值

WBOY
Release: 2016-06-06 20:33:48
Original
1227 people have browsed it

在html上传中需要指定

<code><input type="file" name="media" id="file">
</code>
Copy after login
Copy after login

那么在curl上传文件我该怎么设置其name值为media呢?

<code><?php $ch=curl_init('https://api.weixin.qq.com/cgi-bin/media/upload?access_token=123456&type=image');    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        array('name'=>'media','media'=>'@E:\WWW\6\0604\1.jpg')
    );
    $data=curl_exec($ch);
    curl_close($ch);
    echo $data;
?>  
</code>
Copy after login
Copy after login

回复内容:

在html上传中需要指定

<code><input type="file" name="media" id="file">
</code>
Copy after login
Copy after login

那么在curl上传文件我该怎么设置其name值为media呢?

<code><?php $ch=curl_init('https://api.weixin.qq.com/cgi-bin/media/upload?access_token=123456&type=image');    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        array('name'=>'media','media'=>'@E:\WWW\6\0604\1.jpg')
    );
    $data=curl_exec($ch);
    curl_close($ch);
    echo $data;
?>  
</code>
Copy after login
Copy after login

<code>curl_setopt($ch, CURLOPT_POSTFIELDS, array('media'=>'@E:\WWW\6\0604\1.jpg'));
</code>
Copy after login

等同于

<code><input type="file" name="media">
</code>
Copy after login

数组中的 键 ‘media’ 其实就是 input 中的 name

我是注册了一个账号,进来给您点个赞!!!谢谢

学习了,还真没这么用过呢

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!