将文件从Php发送到web服务

将文件从Php发送到web服务,php,web-services,file,Php,Web Services,File,我有一个web服务,它接受文件作为Post请求,并将其上载到服务器上,然后返回文件路径 该服务通过Ajax请求正常工作。但是现在我想从Php向web服务发出一个请求,它给我的错误是“Undefined index:imageFile” 下面是我尝试使用的代码 $filename = 'e:/e.jpg'; $fields = array('imageFile'=>$filename); $fieldsQueryString = http_build_query($fields); /

我有一个web服务,它接受文件作为Post请求,并将其上载到服务器上,然后返回文件路径

该服务通过Ajax请求正常工作。但是现在我想从Php向web服务发出一个请求,它给我的错误是“Undefined index:imageFile”

下面是我尝试使用的代码

$filename = 'e:/e.jpg';

$fields = array('imageFile'=>$filename);

$fieldsQueryString = http_build_query($fields);

// you should have curl library install here 
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload"); curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
您应该在文件路径之前添加@(eta)符号以上载文件

有关更多信息,请阅读PHP手册中的CURLOPT_POSTFIELDS设置

<?php
$filename = '@e:/e.jpg';
$fields = array('imageFile'=>$filename); 
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
我必须使用

卷曲文件创建()

对象创建文件并将其发送到web服务。就这样做了,它的工作就像一个魅力

谢谢