0

I am trying to include the Content-Length HTTP header in a call and I am struggling to get the length of the XML request string. I have sent a request with the API explorer which returned Content-Length in its response headers.

So I copied the XML and attempted to get the length from it with code as I know the length is 575, but the best I can do is return the length as 567. The code I used is below, if anyone can point me in the correct direction I will be very grateful.

  $testXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <GetFeedbackResponse
      xmlns=\"urn:ebay:apis:eBLBaseComponents\">
      <Timestamp>2017-08-08T19:52:42.536Z</Timestamp>
      <Ack>Failure</Ack>
      <Errors>
        <ShortMessage>User not found.</ShortMessage>
        <LongMessage>The user &quot;XXXXX&quot; was not found in our database.</LongMessage>
        <ErrorCode>21</ErrorCode>
        <SeverityCode>Error</SeverityCode>
        <ErrorParameters ParamID=\"0\">
          <Value>XXXXX</Value>
        </ErrorParameters>
        <ErrorClassification>RequestError</ErrorClassification>
      </Errors>
      <Version>989</Version>
      <Build>E989_INTL_API_18131002_R1</Build>
    </GetFeedbackResponse>";
    //length is 575 according to eBay

    $testXml = new \SimpleXMLElement($testXml);
    $testXml = $testXml->asXML();
    $testXml = str_replace(" ", "", $testXml);
    echo strlen($testXml);
    //echos length 567
pushkin
  • 9,575
  • 15
  • 51
  • 95
JBstar
  • 37
  • 6
  • Are you sure you need to set the content-length header? – Aron Aug 08 '17 at 21:38
  • Hello Aron. You don’t have to set it but its recommended. I am just really puzzled as to how eBay have got the above xml as length 567. – JBstar Aug 08 '17 at 21:45
  • There happen to be 8 escaped double quotes in the XML, so maybe they're counting them as one character - i.e. the length of this is 3, not 5: `\"0\"` – Aron Aug 08 '17 at 21:48
  • Very well spotted Aron. Think you may have cracked it there thank you very much. – JBstar Aug 08 '17 at 22:06
  • Cool, thanks. For posterity I moved it to be an answer. – Aron Aug 08 '17 at 22:11
  • But thinking about that I would be up wouldn’t I not down? – JBstar Aug 08 '17 at 22:12
  • OK, I deleted the answer. Another possibility would be that they're counting `"XXXXX"` differently. And since your code is replacing the spaces, it might never reconcile. Or it could be the converse... Since you're converting the string to an XML object, maybe that's counting the escaped quotes as 1 instead of 2. – Aron Aug 08 '17 at 22:15
  • Mmm yes thank you Aron I will have a look at that and let you know, thank you for your input it has been very helpful. – JBstar Aug 08 '17 at 22:33
  • By any chance, are you using multibyte characters? `strlen()` is counting octets, not characters. This is correct, btw: HTTP is interested into the number of bytes. – DaSourcerer Aug 08 '17 at 23:30
  • I am not 100% sure to be honest. I have tried encoding strlen() as utf8 and base64 which has made no difference. I just can't seem to match the content-length ebay are expecting for my xml. – JBstar Aug 09 '17 at 08:54
  • `Content-Length` is expected to contain the bytecount in decimal numbers. Not sure why you would want to UTF8 or Base64 that. Again: Is the code you "know" counting the characters or the bytes? – DaSourcerer Aug 09 '17 at 12:30
  • Thank you for all you help I have solved this now. I didn’t need to get the same size in bytes as eBay just needed to ensure the size was correct for my call. As sizes in bytes may differ for numerous reasons in my application for an identical piece of XML on another machine. A simple strlen() worked for this. – JBstar Aug 10 '17 at 21:54

0 Answers0