我需要发送以base64编码的XML文件以及变量$content
中的其他数据。
我不确定Content-Length的计算方式是否正确,因为我的Web服务会告诉我错误(无效的参数输入)。我确定文件发送正确。所以我唯一的疑问是长度计算出来了吗?
这是我的脚本:
$base64 = base64_encode(file_get_contents("file.xml"));
$post = "/services/invoice/upload";
$content = array(
"dataFile" => $base64,
"credential" => "",
"domain" => ""
);
try {
$ch = curl_init();
$header = array(
"Content-type: application/json;charset=UTF-8",
"Accept: application/json",
"Authorization: Bearer nfsedkvcsjocnso", //token
"Content-Length:" . strlen(json_encode($content)), //
"Host: localhost:8080"
);
curl_setopt($ch, CURLOPT_URL, "http://xxxxxxx.com" . $post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// receive server response
$server_output = curl_exec($ch);
if ($server_output === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
var_dump($server_output);
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf('Curl failed with error #%d: %s',$e->getCode(), $e->getMessage()), E_USER_ERROR);
}
这是WS的规范(示例):
POST /services/invoice/upload HTTP/1.1
Accept: application/json
Authorization: Bearer NLOGDVXLVaF3tzmnVPkTwpkuh7dG0i09uSCcog3u+rE=
Content-Type: application/json;charset=UTF-8
Content-Length: 90
Host: localhost:8080
{
"dataFile" : "dGVzdA==",
"credential" : "cred_firma", //can be empty
"domain" : "dom_firma" //can be empty
}