Jump to content

How to handle notices when parsing XML and the element doesn't exist..??


angelleye

Recommended Posts

I'm working with PayPal's API, specifically the Express Checkout portion.  I've very experienced with it, but not as experienced with PHP.

 

The first call you make during Express Checkout has the option to send <Custom> and <InvoiceID>.  If you send it they will be returned in the details response later on (the second call in the process.)

 

I have functions setup to handle each call.  I'm actually treating their SOAP API just like XML over HTTP rather than working with .wsdl files because I simply haven't figured that out yet.  Anyway, I make the call and then I parse the response using DOMDocument() and getElementsByTagName().  The problem occurs when <Custom> and <InvoiceID> don't get sent originally and they don't come back later.  I wind up with notices: Trying to get property of non-object.

 

Here's a look at the SOAP/XML response I'm working with..

 

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:market="urn:ebay:apis:Market" xmlns:auction="urn:ebay:apis:Auction" xmlns:sizeship="urn:ebay:api:PayPalAPI/sizeship.xsd" xmlns:ship="urn:ebay:apis:ship" xmlns:skype="urn:ebay:apis:skype" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI">
  <SOAP-ENV:Header>
    <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"></Security>
    <RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
      <Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
        <Username xsi:type="xs:string"></Username>
        <Password xsi:type="xs:string"></Password>
        <Signature xsi:type="xs:string"></Signature>
        <Subject xsi:type="xs:string"></Subject>
      </Credentials>
    </RequesterCredentials>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body id="_0">
    <GetExpressCheckoutDetailsResponse xmlns="urn:ebay:api:PayPalAPI">
      <Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2008-02-17T03:08:54Z</Timestamp>
      <Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack>
      <CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">bc959a8774e4b</CorrelationID>
      <Version xmlns="urn:ebay:apis:eBLBaseComponents">50.000000</Version>
      <Build xmlns="urn:ebay:apis:eBLBaseComponents">512684</Build>
      <GetExpressCheckoutDetailsResponseDetails xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:GetExpressCheckoutDetailsResponseDetailsType">
        <Token xsi:type="ebl:ExpressCheckoutTokenType">EC-07T88228A7173890V</Token>
        <PayerInfo xsi:type="ebl:PayerInfoType">
          <Payer xsi:type="ebl:EmailAddressType">drew_angell@yahoo.com</Payer>
          <PayerID xsi:type="ebl:UserIDType">USXPZMPA94YQ6</PayerID>
          <PayerStatus xsi:type="ebl:PayPalUserStatusCodeType">verified</PayerStatus>
          <PayerName xsi:type="ebl:PersonNameType">
            <Salutation xmlns="urn:ebay:apis:eBLBaseComponents"></Salutation>
            <FirstName xmlns="urn:ebay:apis:eBLBaseComponents">Drew</FirstName>
            <MiddleName xmlns="urn:ebay:apis:eBLBaseComponents"></MiddleName>
            <LastName xmlns="urn:ebay:apis:eBLBaseComponents">Angell</LastName>
            <Suffix xmlns="urn:ebay:apis:eBLBaseComponents"></Suffix>
          </PayerName>
          <PayerCountry xsi:type="ebl:CountryCodeType">US</PayerCountry>
          <PayerBusiness xsi:type="xs:string"></PayerBusiness>
          <Address xsi:type="ebl:AddressType">
            <Name xsi:type="xs:string">Drew Angell</Name>
            <Street1 xsi:type="xs:string">34234 E. 50th St.</Street1>
            <Street2 xsi:type="xs:string"></Street2>
            <CityName xsi:type="xs:string">Kansas City</CityName>
            <StateOrProvince xsi:type="xs:string">MO</StateOrProvince>
            <Country xsi:type="ebl:CountryCodeType">US</Country>
            <CountryName></CountryName>
            <PostalCode xsi:type="xs:string">64111</PostalCode>
            <AddressOwner xsi:type="ebl:AddressOwnerCodeType">PayPal</AddressOwner>
            <AddressStatus xsi:type="ebl:AddressStatusCodeType">Confirmed</AddressStatus>
          </Address>
          <ContactPhone xsi:type="xs:string"></ContactPhone>
        </PayerInfo>
        <BillingAgreementAcceptedStatus>false</BillingAgreementAcceptedStatus>
      </GetExpressCheckoutDetailsResponseDetails>
    </GetExpressCheckoutDetailsResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Here is how I'm parsing the data, which I was thinking would handle when the element didn't exist...

 

$XMLDOM = new DOMDocument();
$XMLDOM -> loadXML($SOAPResponse);

$Custom = $XMLDOM -> getElementsByTagName("Custom");
if(isset($Custom))
      $Custom = $Custom -> item(0) -> nodeValue;
else
      $Custom = "";
                        
$InvoiceID = $XMLDOM -> getElementsByTagName("InvoiceID");
if(isset($InvoiceID))
      $InvoiceID = $InvoiceID -> item(0) -> nodeValue;
else
      $InvoiceID = "";

 

The error is actually occurs on the line that uses ->item(0) -> nodeValue.  So apparently $InvoiceID and $Custom are getting set (and passing the isset() check) but then that element isn't actually in the response data so it can't parse it.  All of the rest of them are working beautifully as long as the element actually exists in the response, even if it's a blank element. 

 

Any information on how to do this correctly would be greatly appreciated.  Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.