Search code examples
phpxmlwordpressxml-rpcapi-design

How to acces wordpress functions in self-written php code?


I am working on a project where users can check and edit their own data and can also view their invoices. I am writing the code for this project myself in PHP on my local machine but the invoices are made with the wp-invoice plugin on an existing and working wordpress site that's hosted somewhere else.

I want users to be able to log in to my web-app and then see their invoices that the web-app retreived from the wp-invoice api on the public wordpress site. The documentation for the wp-invoice api can be found here: https://www.usabilitydynamics.com/product/wp-invoice/docs/wp-invoice-api, it uses XML-RPC with which I am not familiar.

XML-RPC uses built-in wordpress functions to use the API. So I also downloaded and installed wordpress on my local machine and almost got it working (i believe). But when I load the wordpress functions into my web-app using "wp-load.php" it redirects me to the wordpress installer which says wordpress is already installed and there is a working wp-config.php file. This is correct because I installed wordpress successfully.

My code: (This is my only code, there are no other files, functions or classes included using 'include_once')

define("ABSPATH", "C:/wamp64/www/mijnDashboard/WP");
define("WPINC", "/wp-includes");
include_once( ABSPATH . '/wp-load.php' );
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
                $client = new WP_HTTP_IXR_Client( 'http://MyWordpressSiteWithWP-INVOICEinstalled/xmlrpc.php' );
                $client->query('wp.invoice', array(
                    $method = 'get_invoice',
                    $credentials = array('Username', 'Password'),
                    $args = array(
                        'ID' => 1032017043
                    )
                ));
                $the_invoice = $client->getResponse();
echo "$the_invoice";
echo "hallo";

Result: (it redirected me from my self-written 'dashboard.php' to the wordpress page 'wp-admin/setup-config.php') Result of code

Tried solutions:
I searched the web for solutions and some people said it had something to do with my browser cache. I tried another browser and incognito mode in chrome but both got me the same result.
Beside that I tried to load the wordpress functions that I need to use XML-RPC using an other wordpress file called 'wp-blog-header.php' but it got me the same result.

I actually think this is easy to fix but I am new to XML-RPC and API's at all and I don't know how to properly include all wordpress functions in a non-wordpress, self-written file.

Thanks,

Elias


Solution

  • Okay, I figured it out. I didn't use my local machine anymore but I used a webserver instead. There are no errors and I am not being redirected when I load 'wp-load.php'.

    It is strange that almost the same code runs fine on a public webserver but not on a local machine. But it's working now so it's fine.

    The code: (I used this and uploaded it to the WordPress root directory which contains the other files like wp-config.php etc.)

    <?php
    define( 'WP_USE_THEMES', false );
    require( 'wp-load.php' );
    $rootD = $_SERVER['DOCUMENT_ROOT'];
    define("ABSPATH", "$rootD/httpdocs");
    define("WPINC", "/wp-includes");
    
    include_once( ABSPATH . WPINC . '/class-IXR.php' );
    include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
    
    $client = new WP_HTTP_IXR_Client( 'http://mywordpresswebsite/xmlrpc.php' );
    
    $client->query('wp.invoice', array(
        $method = 'get_invoice',
        $credentials = array('username', 'password'),
        $args = array(
            'ID' => 1032017039
        )
    ));
    
    $the_invoice = $client->getResponse();
    
    foreach ($the_invoice as $key => $value) {
      echo "$value";
    }
    
    ?>
    

    Thanks for all the help.