直到今天,我一直在使用以下脚本,没有任何问题。我换了linux的发行版,不知道有没有关系
$full = realpath($newPath."/".$image);
$data = array('name' => $name, 'pic' => '@'.$full.";type=image/jpeg");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$contents = curl_exec($ch);
我通过 php 的表单发布信息,但我无法提交图像,因为它没有被检测为图像,它被检测为“application/octet-stream”
如何将内容类型指定为图像并同时提交文本?
PD我查看了我之前提出的类似问题,我只是找到了一个具有确切目的的问题并且没有任何回复。我不是要下载任何图片,我是想用几个文本字段填写一个表格,我应该通过同一个表格上传一张图片
最佳答案
好的,所以我找到了问题所在。您需要使用 CurlFile() 类,而不是 '@' 。 $file_name 示例。
文档:http://php.net/manual/en/class.curlfile.php
代替
$postfields['file_contents'] = '@' . $filename;
你会想要这样做
$postfields['file_contents'] = new CurlFile($filename);
您可以查看文档以在构造函数的另一端指定 mime 类型和名称,但这是它的基本要点。
祝你好运!
关于php - 使用 curl 和 php 脚本上传图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38922677/