Thread: Parsing XML

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Thanks, I changed it to use htmlentities() though because when echoing the url with url encode() a xml.sax something something premature end of file error came up but now it doesn't when using htmlentities. So that's all gravy but now I get this error,

    Catchable fatal error: Object of class DOMNodeList could not be converted to string in...

    So I'm thinking that it's still not reading the xml document or something. When I enter the url manually by putting it in the browser's url field and pressing enter, the new xml doc with the rates and total charge comes up fine. when echoing out $results I get this error,

    org.xml.sax.SAXParseException: Premature end of file.

    Here is the code I have now,

    PHP Code:
    $xml '
                    weight_system="IMPERIAL" shipper_number="000222000" destination_postal_code="'
    .$data5['zip'].'" service_type="1"
                '
    ;
                
                
    $xml2 '
                    total_pieces="'
    .$value.'" total_weight="'.$weight.'"
                '
    ;         
                        
                
    $token 'RJ5K7KKKKK; 
                $base_url = '
    https://www.shippingco.com/XML/RatingXML.jsp'; 

                
    $request_url $base_url '?'.htmlentities('shipment=<shipment ' $xml '><total ' $xml2 '/></shipment>&token=' $token);
                
    $result file_get_contents($request_url);
                
                echo 
    $result;
                
                
    $xmlGet = new DOMDocument($result);
                
    $searchNode $xmlGet->getElementsByTagName('rate');
                
                echo 
    $searchNode;         
                
    $attrs = array(); 
                for (
    $i 0$i $searchNode->attributes->length; ++$i) { 
                      
    $node $searchNode->attributes->item($i); 
                      
    $attrs[$node->nodeName] = $node->nodeValue
                } 
                
    var_dump($attrs); 
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    You cannot echo an object which is what getElementsByTagName returns. Use var_dump instead. Sometimes getting the debug information to solve a problem can be as much trouble as solving the problem itself.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok so I did var_dump($result) instead of echoing out $result and got this error,

    string(53) "org.xml.sax.SAXParseException: Premature end of file."
    Catchable fatal error: Object of class DOMNodeList could not be converted to string in...

    What does those errors mean or are referring to?
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    I misread what you had and $result should indeed have been a string. Try this where you echo $result:
    PHP Code:
    if ($result===false) {
       echo 
    "Error retrieving URL: $http_response_header");
    }
    else {
       echo 
    $result;

    Last edited by gw1500se; July 16th, 2013 at 02:21 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Hey I'm getting the same error message and nothing is getting echoed when using the code you gave above. When echoing the $request_url variable, it shows the url like it's supposed to look, when I copy and paste that url echoed into a new tab in the url part of the browser and enter, it pulls up xml code from the shipping company with all the different rates and most importantly the total_charge for shipping so maybe my code where its,

    $result = file_get_contents($request_url);

    Could it be that the error is getting thrown because the url has https instead of http?
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    Not possible. You must be getting either the error message echo or the correct result. Those are the only 2 possible returns from file_get_contents. The error must be occurring somewhere else. Post the code you are using now. Https vs http is irrelevant.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Well I'm still getting the same error messages and nothing echoed so I don't know, apparently it is possible, heres what I have for code now,

    PHP Code:
    $xml '
                    weight_system="IMPERIAL" shipper_number="000222000" destination_postal_code="'
    .$data5['zip'].'" service_type="1"
                '
    ;
                
                
    $xml2 '
                    total_pieces="'
    .$value.'" total_weight="'.$weight.'"
                '
    ;         
                        
                
    $token 'A3EF4RRTTT'
                
    $base_url 'https://www.shippingco.com/XML/RatingXML.jsp'

                
    $request_url $base_url '?'.htmlentities('shipment=<shipment ' $xml '><total ' $xml2 '/></shipment>&token=' $token);
                
    $result file_get_contents($request_url);
                
                if (
    $result === false) { 
                       echo 
    "Error retrieving URL: $http_response_header"
                } else { 
                       echo 
    $result
                } 
                
                
    $xmlGet = new DOMDocument($result);
                
    $searchNode $xmlGet->getElementsByTagName('rate');
                
                echo 
    $searchNode;         
                
    $attrs = array(); 
                for (
    $i 0$i $searchNode->attributes->length; ++$i) { 
                      
    $node $searchNode->attributes->item($i); 
                      
    $attrs[$node->nodeName] = $node->nodeValue
                } 
                
    var_dump($attrs); 
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    what is supposed to go in the $http_response_header variable that you put in your previous post
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    That is the URL server response code returned by file_get_contents. At this point I'd suggest you look at the httpd error log on both servers.

    P.S. Make sure you have all errors enabled:
    PHP Code:
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    I have this code to display errors, I'd thought this method displays all errors but I'll try the code for error reporting you gave too and see if it makes a difference and then post back here if I get the same error or not.
  20. #26
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Yep same errors,

    org.xml.sax.SAXParseException: Premature end of file.
    Catchable fatal error: Object of class DOMNodeList could not be converted to string
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    Assuming you are using the code I have suggestion, then what you are looking at must be contents of $result. That is unless the error is really coming from somewhere else. Modify the debug thus:
    PHP Code:
    echo "Before file_get_contents<br />";
    $result file_get_contents($request_url);
    echo 
    "After file_get_contents<br />";       
                if (
    $result === false) { 
                       echo 
    "Error retrieving URL: $http_response_header"
                } else { 
                       echo 
    $result
                }  
    echo 
    "After results test<br />"
    If that is the value of $result, as I suspect, then you are looking at the wrong place and you need to investigate why the remote server is returning that error rather than your code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #28
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Could this be happening because the connection is done through mysql and not PDO?
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,401
    Rep Power
    594
    Probably not but you must switch to PDO in any case. I cannot draw any conclusions until we isolate where the error occurs.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  28. #30
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok so, I did some debugging and found a really useful piece of code btw, header('Content-type: application/xml'); which helped me find alot of mistakes I was making in my markup. I switched the code from using file_get_contents to using cURL however I'm still at the same spot as before. When var_dump($result) I get bool(false) and echoing $result shows nothing. So I searched up on that and there was a post on a thread from someone saying to make sure to check with the php.ini to see if cURL is activated. So I opened up a ticket with my host and asked them if it was activated in the php.ini file and they said yes it is and I can use cURL through cron jobs or directly under the command line. I'm not sure how to do either of those and apparently by using cURL directly under the command line, they mean I can establish an SSH connection to my account and use curl command for my purpose. So now I'm wondering, does the cURL code I'm trying to use on the actual webpage have no use since I'm only able to use curl through cron jobs or ssh or can my code on the webpage and ssh or whatever be used together. Getting more confused as I go deeper into this but I would really like to figure out how to accomplish retrieving the xml code and getting the total_charge attribute value to display on my webpage so the customer knows exactly what the shipping cost will be. The reason I decided to use curl instead was because I couldn't find any answers to why file_get_contents wasn't working except that it doesn't handle https properly. Maybe it does though but I don't know how to make it accept a secured url and retrieve the xml code from the page of that secured url. Here is the code I have now,

    PHP Code:
    $xml '
                    weight_system="IMPERIAL" shipper_number="000222000" destination_postal_code="'
    .$data5['zip'].'" service_type="1"
                '
    ;
                
                
    $xml2 '
                    total_pieces="'
    .$value.'" total_weight="'.$weight.'"
                '
    ;         
                        
                
    $token 'A1W2EEEDDD'
                
    $base_url 'https://www.shippingco.com/XML/RatingXML.jsp'

                
    $request_url $base_url '?'.htmlentities('shipment=<shipment ' $xml '><total ' $xml2 '/></shipment>&token=' $token);
                
                
    // Get cURL resource
                
    $curl curl_init();
                
    // Set some options - we are passing in a useragent too here
                
    curl_setopt_array($curl, array(
                    
    CURLOPT_SSL_VERIFYPEERfalse,
                    
    CURLOPT_SSL_VERIFYHOSTfalse,
                    
    CURLOPT_RETURNTRANSFER => 1,
                    
    CURLOPT_URL => $request_url,
                    
    CURLOPT_USERAGENT'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
                    
    CURLOPT_FAILONERROR => false
                
    ));
                
    // Send the request & save response to $resp
                
    $result curl_exec($curl);
                
    // Close request to clear up some resources
                
    header('Content-type: application/xml');
                
    curl_close($curl);
                
                
    $xmlGet = new DOMDocument($result);
                
    $searchNode $xmlGet->getElementsByTagName('rate');
                        
                
    $attrs = array(); 
                for (
    $i 0$i $searchNode->attributes->length; ++$i) { 
                      
    $node $searchNode->attributes->item($i); 
                      
    $attrs[$node->nodeName] = $node->nodeValue
                } 
                
    var_dump($attrs); 
    and the errors are still as follows,

    Undefined property: DOMNodeList::$attributes in...
    Trying to get property of non-object in...

    and bool(false) when doing var_dump($result)

    Thanks for your time and help, really appreciate it!
    Last edited by andy1212; July 17th, 2013 at 11:12 PM.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo