dongshuogai2343
2019-04-16 18:23
浏览 198
已采纳

POST数据中的CURLFile对象使我的请求失败

I am trying to send an image file to an API through curl in php, but when I try to give my request the file I need, it proceeds to fail when I call curl_exec on it.

The path I'm providing to the file is valid, I used var_dump on my CURLFile object when I created it and it seems fine. When curl_exec is called the request instantly fails and returns false. But, if I replace the CURLFile object by a string in CURLOPT_POSTFIELDS the request works perfectly.

$ch = curl_init('http://localhost/test/test.php');
$curlfile = new CURLFile($path,'image/png','image');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array('image' => $curlfile));
curl_exec($curl);//Returns false
$ch = curl_init('http://localhost/test/test.php');
$curlfile = new CURLFile($path,'image/png','image');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array('image' => "somestring"));
curl_exec($curl);//Returns true
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangejian6657 2019-04-16 18:57
    已采纳

    The CURLFile constructor needed the full path of my file, using realpath() solved my issue.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题