The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

SimpleXml doesnt work

Discussion in 'cPanel Developers' started by B12Org, Dec 16, 2011.

  1. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    Hi,

    I am writing a plugin/custom page for cpanel and it basically makes an SSL call to an external web service using cURL and then converts the response into XML.

    PHP:

            $ch 
    curl_init();
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
            
            
    $body curl_exec($ch);
            
    $errno curl_errno($ch);
            
    $errstr curl_error($ch);
            
    curl_close ($ch);

            
    $results = new SimpleXmlElement($body);

            foreach( 
    $results ->node1->subNode->children() AS $child ){
                    
    $name $child->attributes()->name;
    When I run this outside of cPanel it works fine. However inside of CPanel it doesn't work at all - and throws an error

    Code:
    Fatal error: Call to a member function children() on a non-object in /tmp/cpanel_phpengine.1324057493.469521IcFAclVRR3 on line 210
    
    I have already enabled curl for php5 and ran "/scripts/makecpphp --force " as this post suggests
    http://forums.cpanel.net/f5/curl-php-apache-whm-161806.html

    And that didn't change the behavior at all.

    These are the options I have enabled
    Code:
    Cpanel::Easy::PHP5: 1
    Cpanel::Easy::PHP5::3_5: 1
    Cpanel::Easy::PHP5::Curl: 1
    Cpanel::Easy::PHP5::CurlSSL: 1
    Cpanel::Easy::PHP5::FTP: 1
    Cpanel::Easy::PHP5::GD: 1
    Cpanel::Easy::PHP5::Gettext: 1
    Cpanel::Easy::PHP5::Imap: 1
    Cpanel::Easy::PHP5::Mbstring: 1
    Cpanel::Easy::PHP5::Mcrypt: 1
    Cpanel::Easy::PHP5::Mysql: 1
    Cpanel::Easy::PHP5::MysqlOfSystem: 1
    Cpanel::Easy::PHP5::Openssl: 1
    Cpanel::Easy::PHP5::Pgsql: 1
    Cpanel::Easy::PHP5::Sockets: 1
    Cpanel::Easy::PHP5::XmlRPC: 1
    Cpanel::Easy::PHP5::XsltSablot: 1
    Cpanel::Easy::PHP5::Zip: 1
    Cpanel::Easy::PHP5::Zlib: 1
    
    If I do a var dump on the 3 items all of them are blank
    PHP:
            $body curl_exec($ch);
            
    var_dump($body);

            
    $errno curl_errno($ch);
            
    $errstr curl_error($ch);
            
            
    var_dump($errno);
            
    var_dump($errstr);
    Code:
    string(951) " " int(0) string(0) "" 
    
    which leads me to believe that its not actually making the cURL call.
     
    #1 B12Org, Dec 16, 2011
    Last edited: Dec 16, 2011
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  3. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    Code:
    Fatal error: Call to a member function children() on a non-object in ...
    is probably from
    Code:
    $results ->node1->subNode->children()
    .

    That chain assumes there will always be children in the sub node. (i.e. that subNode() will always return an object).

    When there aren't any children your code is calling children() with a "non-object" per the error.

    Along the same lines:

    * if node1() ever returned false then you'd get something like:
    Code:
    Fatal error: Call to a member function subNode() on a non-object in ...
    * if $results was not an object then you;d get something like:
    Code:
    Fatal error: Call to a member function node1() on a non-object in ...
     
    #3 cPDan, Dec 19, 2011
    Last edited: Dec 19, 2011
  4. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    There could be a few reasons for this.
    * The PHP binary is different from the system one (the one that was built via /scripts/makecpphp **)
    * The environment in cPanel is different from the command line or apache.

    A good start might be had by adding this you your code: (or using fopen to sidestep curl altogether)
    Code:
     curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
    
    then executing it "outside of cPanel" using the system PHP and cPPHP **.

    I'd recommend verifying that the URL is what you expect "outside of cPanel" and "inside of CPanel".

    You might want to try a non-SSL URL also to rule out any SSL issues.

    You could also side step curl altogether for debugging and use fopen() (e.g. perhaps a firewall is blocking the request when its done in cPanel due to a certain header or something)

    ** you can find that path by executing
    Code:
    perl -I/usr/local/cpanel -MCpanel::cPPHP -le 'print Cpanel::cPPHP::get_cpphp_path();'
     
    #4 cPDan, Dec 19, 2011
    Last edited: Dec 19, 2011
  5. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    what I am finding is that even though I changed cpanel php to use curl it doesnt seem to stick -

    i set these to 1

    Cpanel::Easy::PHP5::Curl: 1
    Cpanel::Easy::PHP5::CurlSSL: 1


    and run the
    /scripts/makecpphp --force

    and then I check the options again and they are back to 0 in the settings file. and the curl calls still dont work.

    does that indicate a different problem, perhaps with the compiler or some other system setting?
     
  6. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    Also, adding this

    Code:
     curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
    
    Doesnt change anything.

    Additionally I rewrote it to use fsockopen which came straight from a php.net example for fopen

    PHP:
    class HTTPRequest 

        var 
    $_fp;        // HTTP socket 
        
    var $_url;        // full URL 
        
    var $_host;        // HTTP host 
        
    var $_protocol;    // protocol (HTTP/HTTPS) 
        
    var $_uri;        // request URI 
        
    var $_port;        // port 
        
        // scan url 
        
    function _scan_url() 
        { 
            
    $req $this->_url
            
            
    $pos strpos($req'://'); 
            
    $this->_protocol strtolower(substr($req0$pos)); 
            
            
    $req substr($req$pos+3); 
            
    $pos strpos($req'/'); 
            if(
    $pos === false
                
    $pos strlen($req); 
            
    $host substr($req0$pos); 
            
            if(
    strpos($host':') !== false
            { 
                list(
    $this->_host$this->_port) = explode(':'$host); 
            } 
            else 
            { 
                
    $this->_host $host
                
    $this->_port = ($this->_protocol == 'https') ? 443 80
            } 
            
            
    $this->_uri substr($req$pos); 
            if(
    $this->_uri == ''
                
    $this->_uri '/'
        } 
        
        
    // constructor 
        
    function HTTPRequest($url
        { 
            
    $this->_url $url
            
    $this->_scan_url(); 
        } 
        
        
    // download URL to string 
        
    function DownloadToString() 
        { 
            
    $crlf "\r\n"
            
            
    // generate request 
            
    $req 'GET ' $this->_uri ' HTTP/1.0' $crlf 
                
    .    'Host: ' $this->_host $crlf 
                
    .    $crlf
            
            
    // fetch 
            
    $this->_fp fsockopen(($this->_protocol == 'https' 'ssl://' '') . $this->_host$this->_port);
             
    fwrite($this->_fp$req); 
            while(
    is_resource($this->_fp) && $this->_fp && !feof($this->_fp)) 
                
    $response .= fread($this->_fp1024); 
            
    fclose($this->_fp); 
            
            
    // split header and body 
            
    $pos strpos($response$crlf $crlf); 
            if(
    $pos === false
                return(
    $response); 
            
    $header substr($response0$pos); 
            
    $body substr($response$pos strlen($crlf)); 
            
            
    // parse headers 
            
    $headers = array(); 
            
    $lines explode($crlf$header); 
            foreach(
    $lines as $line
                if((
    $pos strpos($line':')) !== false
                    
    $headers[strtolower(trim(substr($line0$pos)))] = trim(substr($line$pos+1));
             
            
    // redirection? 
            
    if(isset($headers['location'])) 
            { 
                
    $http = new HTTPRequest($headers['location']); 
                return(
    $http->DownloadToString($http)); 
            } 
            else 
            { 
                return(
    $body); 
            } 
        } 
    }

    Then calling it this way
    PHP:
    $url "https://my-url-over-ssl.com/?qstring=params
    $r = new HTTPRequest($url); 
    $body = $r->DownloadToString(); 
    var_dump(
    $body);        
    Then with fopen
    PHP:
    $url "https://my-url-over-ssl.com/?qstring=params
    $handle = fopen("$url", "r");
    $body = stream_get_contents($handle);
    fclose(
    $handle);
    var_dump(
    $body);        
    Both throw the same error as before when using CURL over SSL. Changing to non SSL (http) did not change behavior.


    So what gives? Why cant Curl, fopen, fsockopen, pfsockopen work?
    What needs to be done to get this very simple thing (which in all cases works on your competitors panels I might add) to work?

    PS there are no firewall problems contributing - it has been confirmed that the server via command line tools is able to open and retrieve the proper contents of the $url parameter. Additionally this has also been attempted with iptables stopped and no change in behavior.


    One thing I did notice right away though is this
    Code:
    # php -v
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    php: /opt/xml2/lib/libxml2.so.2: no version information available (required by php)
    Segmentation fault (core dumped)
    
    # gdb `which php` core.26722 `which php` --batch --quiet -ex "thread apply all bt full"
    Excess command line arguments ignored. (/usr/local/bin/php)
    [New Thread 26722]
    [Thread debugging using libthread_db enabled]
    Core was generated by `php -i phpinfo'.
    Program terminated with signal 11, Segmentation fault.
    #0  0x000073637154d9ec in _dl_map_object_from_fd () from /lib64/ld-linux-x86-64.so.2
    
    Thread 1 (Thread 0x736371736720 (LWP 26722)):
    #0  0x000073637154d9ec in _dl_map_object_from_fd () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #1  0x000073637154e7d3 in _dl_map_object () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #2  0x0000736371557dd2 in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #3  0x0000736371554076 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #4  0x00007363715577ec in _dl_open () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #5  0x00007363709e5f9a in dlopen_doit () from /lib64/libdl.so.2
    No symbol table info available.
    #6  0x0000736371554076 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
    No symbol table info available.
    #7  0x00007363709e650d in _dlerror_run () from /lib64/libdl.so.2
    No symbol table info available.
    #8  0x00007363709e5f11 in dlopen@@GLIBC_2.2.5 () from /lib64/libdl.so.2
    No symbol table info available.
    #9  0x00000000006dabe5 in zend_load_extension (path=0x736371763000 "") at /home/cpeasyapache/src/php-5.2.14/Zend/zend_extensions.c:34
            handle = <value optimized out>
            new_extension = <value optimized out>
            extension_version_info = <value optimized out>
    #10 0x00000000006c6fd7 in zend_llist_apply (l=<value optimized out>, func=0x694d30 <php_load_zend_extension_cb>) at /home/cpeasyapache/src/php-5.2.14/Zend/zend_llist.c:193
            element = 0xd5c850
    #11 0x0000000000694cd7 in php_ini_register_extensions () at /home/cpeasyapache/src/php-5.2.14/main/php_ini.c:616
    No locals.
    #12 0x000000000068f3e6 in php_module_startup (sf=<value optimized out>, additional_modules=0x0, num_additional_modules=0) at /home/cpeasyapache/src/php-5.2.14/main/main.c:1854
            zuf = {error_function = 0x690250 <php_error_cb>, printf_function = 0x68ec90 <php_printf>, write_function = 0x68e0f0 <php_body_write_wrapper>, fopen_function = 0x68e360 <php_fopen_wrapper_for_zend>,
              message_handler = 0x68fda0 <php_message_handler_for_zend>, block_interruptions = 0, unblock_interruptions = 0, get_configuration_directive = 0x68e330 <php_get_configuration_directive_for_zend>,
              ticks_function = 0x69d080 <php_run_ticks>, on_timeout = 0x68de40 <php_on_timeout>, stream_open_function = 0x68ec50 <php_stream_open_for_zend>, vspprintf_function = 0x692450 <vspprintf>, getenv_function = 0x695fe0 <sapi_getenv>}
            zuv = {import_use_extension = 0x9d241a ".php", import_use_extension_length = 112, html_errors = 1 '\001'}
    #13 0x0000000000753a5d in php_cli_startup (sapi_module=0x736371763000) at /home/cpeasyapache/src/php-5.2.14/sapi/cli/php_cli.c:389
    No locals.
    #14 0x0000000000754201 in main (argc=3, argv=0x7fff6b60f818) at /home/cpeasyapache/src/php-5.2.14/sapi/cli/php_cli.c:748
            exit_status = 0
            c = <value optimized out>
            file_handle = {type = 1 '\001', filename = 0xffffffff <Address 0xffffffff out of bounds>, opened_path = 0x7363712571d8 "O", handle = {fd = 0, fp = 0x0, stream = {handle = 0x0, reader = 0x7363702d8898, closer = 0x7fff6b60f838,
                  fteller = 0x16b60f818, interactive = 1903398960}}, free_filename = 96 '`'}
            behavior = <value optimized out>
            reflection_what = <value optimized out>
            orig_optind = 1
            orig_optarg = 0x0
            arg_free = <value optimized out>
            arg_excp = <value optimized out>
            script_file = <value optimized out>
            interactive = <value optimized out>
            module_started = 0
            request_started = 0
            lineno = 0
            exec_direct = <value optimized out>
            exec_run = <value optimized out>
            exec_begin = <value optimized out>
            exec_end = <value optimized out>
            param_error = <value optimized out>
            hide_argv = <value optimized out>
            ini_entries_len = 1735289202
    
    

    So maybe php is broken on your easy apache install ?
     
    #6 B12Org, Jan 9, 2012
    Last edited: Jan 9, 2012
  7. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    I know this isn't the main problem but did want to clear up some confusion:

    Those options are mutually exclusive so one will be ignored/disabled. CurlSSL is enable in the makecpphp profile cPanel provides.

    If they are both disabled afterward then you're probably not using the cPanel provided profile.
     
  8. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    I am unable to reproduce. If anyone having this problem can create a ticket marked “ATTN: Dan” and referencing this thread, so that I can look into it on an affected box that'd be great!

    It appears to be a low level symbol/linking issue that I suspect will be easily remedied once we can get a look at it in practice.

    thanks
     
  9. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    On an affected system:

    PHP:
    <?php
    $ch 
    curl_init();
            
    curl_setopt($chCURLOPT_URL'https://java.sun.com/developer/earlyAccess/xml/examples/samples/book-order.xml');
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
            
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);

            
    $body curl_exec($ch);
            
    $errno curl_errno($ch);
            
    $errstr curl_error($ch);
            
    curl_close ($ch);

            
    $results = new SimpleXmlElement($body);

           
    var_dump($results);
    ?>
    that code dumped the expected XML results via:

    `/var/cpanel/3rdparty/bin/php 56306.php`

    There were some warnings about extensions but after removing the system PHP extensions from the non-system php.ini the warnings went away.

    Also:

    `/usr/local/cpanel/3rdparty/bin/php 56306.php`

    throws “Fatal error: Call to undefined function curl_init() in /root/56306.php on line 2” but that is fairly common due to PHP linking issues and the reason why its super easy to build one for the system in question via makecpphp.

    As far as cpanel goes it should be using the correct one:

    Code:
    [~]# perl -I/usr/local/cpanel -MCpanel::cPPHP -le 'print Cpanel::cPPHP::get_php_path_for_internal();'
    /var/cpanel/3rdparty/bin/php
    [~]# perl -I/usr/local/cpanel -MCpanel::cPPHP -le 'print Cpanel::cPPHP::get_cpphp_path();'
    /var/cpanel/3rdparty/bin/php
    [~]# 
    
    HTH!
     
    #9 cPDan, Jan 10, 2012
    Last edited: Jan 10, 2012

Share This Page