如何使用curl命令实现图片的极简图床上传?
- 内容介绍
- 文章标签
- 相关推荐
本文共计197个文字,预计阅读时间需要1分钟。
上传图片至指定网站,请按照以下步骤操作:
1. 获取图片内容并编码:php$data=base64_encode(file_get_contents('test.jpg'));
2. 初始化CURL会话并设置选项:php$ch=curl_init('http://yotuku.cn/upload?name=');
3. 设置CURL选项,包括请求方法、编码类型等:phpcurl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);curl_setopt($ch, CURLOPT_POSTFIELDS, data= . $data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($data)));
4. 执行CURL会话并获取响应:php$response=curl_exec($ch);
5. 关闭CURL会话:phpcurl_close($ch);
上传图片至yotuku.cn/,详见:blog.skyx.in/archives/164/1.[代码][PHP]代码
<?php $data = base64_encode(file_get_contents('test.jpg')); $ch = curl_init('yotuku.cn/upload?name='); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text/plain', 'Content-Length: ' . strlen($data)) ); $result = curl_exec($ch); echo $result;
本文共计197个文字,预计阅读时间需要1分钟。
上传图片至指定网站,请按照以下步骤操作:
1. 获取图片内容并编码:php$data=base64_encode(file_get_contents('test.jpg'));
2. 初始化CURL会话并设置选项:php$ch=curl_init('http://yotuku.cn/upload?name=');
3. 设置CURL选项,包括请求方法、编码类型等:phpcurl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);curl_setopt($ch, CURLOPT_POSTFIELDS, data= . $data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($data)));
4. 执行CURL会话并获取响应:php$response=curl_exec($ch);
5. 关闭CURL会话:phpcurl_close($ch);
上传图片至yotuku.cn/,详见:blog.skyx.in/archives/164/1.[代码][PHP]代码
<?php $data = base64_encode(file_get_contents('test.jpg')); $ch = curl_init('yotuku.cn/upload?name='); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text/plain', 'Content-Length: ' . strlen($data)) ); $result = curl_exec($ch); echo $result;

