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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看