1

我正在使用一个名为 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;


    ?>

它返回:保存文件时出现问题。请再试一次。

我做了一些我知道我做过的愚蠢的事情。

4

2 回答 2

1

base64_encode如果传递的参数本身为空,将返回空字符串。在将数据传递给此函数之前,您应该检查这一点。

if(strlen($pdf_data)>0){
   $pdf_data = base64_encode($pdf_data);
}else{
   throw new Exception("Empty File provided");
}
于 2012-12-27T11:37:39.583 回答
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;
于 2012-12-27T11:37:57.443 回答