[英]CURL Warning: curl_file_create() expects parameter 1 to be a valid path, string given
I don't know why this is not working.我不知道为什么这不起作用。 I am trying to download a image from a URL then sent it via CURL.我正在尝试从 URL 下载图像,然后通过 CURL 发送它。 POST request the path with var_dump
is working, but with CURL it doesn't work POST 请求使用var_dump
的路径有效,但使用 CURL 则无效
The error is错误是
Warning: curl_file_create() expects parameter 1 to be a valid path, string given警告:curl_file_create() 期望参数 1 是有效路径,给定字符串
// define('TMP', __DIR__ . DIRECTORY_SEPARATOR . 'tmp');
/**
* @param $url
* @return string
*/
function dlImage($url) {
$save_dir = TMP . DIRECTORY_SEPARATOR;
$filename = basename($url);
$save = $save_dir.$filename;
file_put_contents($save,file_get_contents($url));
return $save;
}
function upload( $body, $title, $type, $type_real, $subCat, $poster = null, $imdbUrl = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'xxxx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$data = [
'key' => 'xxxx',
'type' => $type,
'subtype' => $subCat,
'type_real' => $type_real,
'title' => $title,
'posterUpload' => curl_file_create(realpath(dlImage($poster)),'image/jpeg'),
'imdbUrl' => $imdbUrl,
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
// path is working fine
dlimage output (var_dump) : string(113)
"C:\laragon\www\api\tmp\MV5BNDliY2E1MjUtNzZkOS00MzJlLTgyOGEtZDg4MTI1NzZkMTBhXkEyXkFqcGdeQXVyNjMwMzc3MjE@.jpg"
add more error checking, replace添加更多错误检查,替换
file_put_contents($save,file_get_contents($url));
with和
$content=file_get_contents($url);
if(!is_string($content)){
throw new \RuntimeException("failed to fetch url {$url}");
}
if(($len=strlen($content))!==($written=file_put_contents($save,$content))){
throw new \RuntimeException("could only write {$written}/{$len} bytes to file {$save}");
}
then you should probably get an errorlog explaining where things went wrong, and my best guess is that things went wrong with file_get_contents() and/or file_put_contents().那么你可能应该得到一个错误日志来解释哪里出错了,我最好的猜测是 file_get_contents() 和/或 file_put_contents() 出错了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.