我正在使用一个名为ViaPost的网站为我的公司发信。我正在使用SoapClient。上传的文件必须采用base64binary格式。我正在使用以下代码,但返回空。
$pdf_data = file_get_contents('test.pdf');
$pdf_data = base64_encode($pdf_data);
print $pdf_data;
扩展上述内容:
POST /viapostcustomer.asmx HTTP/1.1
Host: api.viapost.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://api.viapost.com/CreateLetter"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CreateLetter xmlns="http://api.viapost.com">
<loginToken>string</loginToken>
<name>string</name>
<description>string</description>
<FileContents>base64Binary</FileContents>
<dynamic>boolean</dynamic>
<shareLetterWithGroup>boolean</shareLetterWithGroup>
<returnMessage>string</returnMessage>
<letterID>long</letterID>
</CreateLetter>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CreateLetterResponse xmlns="http://api.viapost.com">
<CreateLetterResult>boolean</CreateLetterResult>
<returnMessage>string</returnMessage>
<letterID>long</letterID>
</CreateLetterResponse>
</soap:Body>
</soap:Envelope>
<?php
$client = new SoapClient("http://api.viapost.com/viapostcustomer.asmx?WSDL");
$params = array('sUserName'=>'USERNAME','sPassword'=>'PASSWORD','sLoginToken'=>'','sReturnMessage'=>'');
$SignIn = $client->__soapCall('SignIn', array($params));
$sLoginToken = $SignIn->sLoginToken;
$pdf_data = file_get_contents('test.pdf');
$pdf_data = base64_encode($pdf_data);
$params = array('loginToken'=>$sLoginToken,'name'=>'testname','description'=>'testdescription','fileContents'=>$pdf_data,'dynamic'=>'true','shareLetterWithGroup'=>'false','returnMessage'=>'','letterID'=>'');
$CreateLetter = $client->__soapCall('CreateLetter', array($params));
$returnMessage = $CreateLetter->returnMessage;
print $returnMessage;
?>
它返回:保存文件时出现问题。请再试一次。
我做过一些我知道的事情。
答案 0 :(得分:1)
base64_encode
将返回空字符串。在将数据传递给此函数之前,您应该检查这一点。
if(strlen($pdf_data)>0){
$pdf_data = base64_encode($pdf_data);
}else{
throw new Exception("Empty File provided");
}
答案 1 :(得分:1)
尝试增加错误报告,奇怪的检查失败也不会造成伤害。这应该有所帮助。 :)
<?php
error_reporting(-1);
ini_set('display_errors', true);
$pdf_data = file_get_contents('test.pdf');
if( ! $pdf_data) {
print 'Could not read file';
exit;
}
$pdf_data = base64_encode($pdf_data);
print $pdf_data;