Im doing a direct .php call (ajax). Due to my classes, loading system, I would need to load wp-load later, not at the start.
So, if my file has only this:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
it runs ok. But if its wrapped inside a class:
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
}
}
(new Paff())->x();
it dies with a fatal error:
PHP Fatal error: Call to undefined method stdClass::escape() in /var/www/wp-content/sunrise.php on line 11
I cant see any reason why!
It seems like you're trying to load a WordPress multisite outside of the normal parameters (judging by sunrise.php being the issue here). Try loading this file instead:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
This skips index.php which defines the WP_USE_THEMES constant and assumes you're not loading the front-end of the site (at least not traditionally, which you're not).
That should load everything and in the right order for you now.
I also could not see anything wrong with your code. So I tested it with one change just for confirmation that it does work.
<?php
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
echo 'wp-load.php included. ABSPATH=' . ABSPATH;
}
}
(new Paff())->x();
?>
It runs without error! and displays the following in the browser.
wp-load.php included. ABSPATH=D:\bronce/
I displayed the ABSPATH just to confirm that wp-load.php was included as it defines ABSPATH.
So this works as it should on my installation which is very standard as I just installed PHP a few weeks ago and I know I didn't change the defaults very much. So why does it not work on your system? It must be configured different. Is your system running any pre-execution handlers?
I just did a search for 'sunrise.php' which I originally thought was the name of your code. It may actually be an infection (or not) see https://premium.wpmudev.org/blog/removing-backdoor-exploits/. I think given its name it is configured to run as a pre-execution handler. Some plugins legitimately install pre-execution handlers, e.g. WordFence. Unfortunately, I did not install WordFence this time and I don't recall the name of its pre-execution handler.
I just read the source code to WordFence. It does install a pre-execution handler but its name is ABSPATH . 'wordfence-waf.php'. However, WordFence also has a reference to '.../wp-content/sunrise.php' apparently executed before multisite loading. Are you using WordFence? Is your site a multisite?
The important point is: The problem is not in your code but in a pre-execution handler. A pre-execution handler can be a huge amount of PHP code that can significantly change the PHP execution environment, which is why plugins such as WordFence use it to monitor your code execution.
I'm getting an error that class is not found, but I clearly have the right path for where it is located:
<?php
require_once('stripe-php-2.1.0/stripe/lib/Stripe.php');
Stripe::setApiKey('my_key');
var_dump($_POST['stripe-token']);
?>
Every article I've come across all claim that the problem is (not including the right path) in the require_one, include, or require. (I've tried all 3). But still no luck. My database calls follow the same format and my WAMP server has no problem creating my database class.
This is copied directly from my file explore (copy paste)
website\stripe-php-2.1.0\stripe\lib\Stripe.php
My php file that I am using to try and access Stripe sits in the same place as 'website'.
PHP version 5.5.12
tutorial references: http://www.larryullman.com/2013/01/09/writing-the-php-code-to-process-payments-with-stripe/
Other reference: http://www.youtube.com/watch?v=Lka_JBM9bbY
It's because it uses a namespace. Try:
\Stripe\Stripe::setApiKey('my_key');
It is better to initialize all classes.
require_once ("stripe_folder/init.php");
then use namespaces:
\Stripe\Stripe::setApiKey('key_key_key_key_key_key');
I've created a phar of a Symfony2 web application, but I'm having some troubles with the cache-folders.
I found out that I could mount an external file/folder into a phar. That would fix my issue, but I can't get the example on the PHP site working.
I have a phar which contains an index.php:
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
Then I include the .phar with the following code:
<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
// now run the application
include 'phar-archive.phar';
?>
All files exists, but I get the following error:
PHP Fatal error: Uncaught exception 'PharException' with message 'config.xml is not a phar archive, cannot mount' in /var/www/$projectname/index.php:3
I already tried relative/absolute paths, changing permissions but can't get this to work. Additionally a working example of how I could mount a folder into a phar would be great !
First check that variable $projectname exist in this place (just run echo $projectname; before Phar::mount). If all ok, then change
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
to
Phar::mount('phar://config.xml', "/var/www/{$projectname}/config.xml");
It seems that variable $projectname not converted to its value because you used single quotes.
I've written a simple PHP script which instantiates a COM object for an out-of-process (i.e. exe file) COM component and uses it to call a COM method that the component exposes. This COM method very simply quadruples the number passed as the first argument, returning the result in the second argument (passed by reference). The script shown below works successfully on my local development machine on WampServer 2.0 (Apache 2.2.11 / PHP 5.3.1). The COM component is a Win32 executable built using Delphi.
<?php
// ensure no time limit is imposed
set_time_limit(0);
// show all errors, warnings and notices whilst developing
error_reporting(E_ALL);
$numIn = 3;
$numOut = new VARIANT(1, VT_I4);
echo '----- BEFORE ---------' . '<br>';
echo 'NumIn: ' . $numIn . '<br>';
echo 'NumOut: ' . $numOut . '<br>';
echo '----------------------' . '<br>';
$oleapp = new COM("OleAutomationFeasibilityModel.Automation") or die ("Could not initialise feasibility model object.");
echo '<br />COM object created version = ' . $oleapp->Version . '<br /><br />';
$oleapp->CalculateWithVariants($numIn, $numOut);
unset($oleapp);
echo '----- AFTER ---------' . '<br>';
echo 'NumIn: ' . $numIn . '<br>';
echo 'NumOut: ' . $numOut . '<br>';
echo '----------------------' . '<br>';
?>
Note: as I understand it, one can only pass a parameter by reference to a COM method using a VARIANT type, as common data types like integers and strings won't work (see http://www.php.net/manual/en/ref.com.php#45038).
I then created and deployed an Azure Web Role (Cloud Service) with a startup script that registers the COM component successfully i.e. the appropriate registry keys appeared in the registry. To further confirm that the COM component could be interacted with, I used RDP to connect to the cloud service instance and installed Microsoft Access Runtime 2010 as I have an Access application that provides a GUI to test the methods of the COM component. I was able to run this application and successfully interacted with the COM component, using it to pass an integer to the CalculateWithVariants method and the expected quadrupled result was returned. So, I've established that the COM component is installed and can be interacted with on the Azure cloud service instance.
Next I included the above PHP script in the Web Role and deployed it on Azure. Unfortunately, calling the script from a browser results in an HTTP Error 500 (Internal Server Error) and I'm struggling to find out why. If I comment out all lines referencing $oleapp, I still get the same error. If I additionally comment out the line that instantiates a variant object, no error occurs. If I reinstate the line which instantiates the COM object and the line below it, I receive no error message but the only text echoed is from the lines preceding the COM object creation line i.e. the call to the Version method fails. So it appears to be struggling with the variant object creation and the COM object creation.
I'm a bit stuck in terms of how to resolve this issue. I would therefore be very grateful if anyone has any pointers as to a way forward.
UPDATE 1
I decided to try a different course of action on the Azure platform by...
creating an Azure Virtual Machine with a Windows Server 2008 R2 OS
installing WampServer 2.2E (Apache 2.2.22 / PHP 5.3.13 / MySQL
5.5.24) in the VM as a quick and easy way to test whether this approach would work
copying the above PHP script into the WampServer "www directory"
launching WampServer
selecting the "Put Online" option from the WampServer Menu (accessed by left-clicking the WampServer icon in the Windows Taskbar Notification area)
creating an "Inbound Rule" for the VM firewall to allow connections to port 80
...and thankfully the script ran successfully!
Ideally, I would still like to get this working as an Azure cloud service as it shouldn't be necessary for me to maintain the PHP installation in a full VM.
UPDATE 2
I tried restarting the cloud service, then remotely connecting to an instance of the cloud service and looking in the Application Event Viewer. I saw that WMI logged 1 error during startup:
Event filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60
WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99"
could not be reactivated in namespace "//./root/CIMV2" because of error 0x80041003
Events cannot be delivered through this filter until the problem is corrected.
I then ran the above script a couple of times and rechecked the Application Event Viewer but nothing had been logged.
I also checked the IIS logs and the Azure log, startup-tasks-log and startup-tasks-error-log files to no avail.
After giving up on solving this last year. I made another concerted effort to resolve it this week and succeeded!
I basically needed to (a) enable the php_com_dotnet.dll to allow use of COM and VARIANT classes, and (b) grant default Local Activation permission to IIS_IUSRS to allow access to the COM component. I've listed the detailed steps I took below...
Add a folder called php in the web role's bin folder
As of PHP 5.3.15 / 5.4.5, in order to use the COM and VARIANT
classes, the php_com_dotnet.dll needs to be enabled inside of
php.ini. Previous versions of PHP enabled these extensions by
default (source: http://www.php.net/manual/en/com.installation.php). In
the php folder, create a php.ini file containing only the following
lines...
[COM_DOT_NET]
extension=php_com_dotnet.dll
Create a SetDCOMPermission.reg file in the bin folder which contains the following content, to grant default Local Activation permission to IIS_IUSRS...
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OLE]
"DefaultLaunchPermission"=hex(3):01,00,04,80,74,00,00,00,84,00,00,00,00,00,\
00,00,14,00,00,00,02,00,60,00,04,00,00,00,00,00,14,00,1F,00,00,00,01,01,00,\
00,00,00,00,05,12,00,00,00,00,00,18,00,1F,00,00,00,01,02,00,00,00,00,00,05,\
20,00,00,00,20,02,00,00,00,00,18,00,0B,00,00,00,01,02,00,00,00,00,00,05,20,\
00,00,00,38,02,00,00,00,00,14,00,1F,00,00,00,01,01,00,00,00,00,00,05,04,00,\
00,00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,02,00,00,00,00,00,\
05,20,00,00,00,20,02,00,00
I don't know if the above registry change will work for everyone, so the process I used is documented here (it essentially involved using a program called RegFromApp to record the changes made to the registry when granting default Local Activation permissions for IIS_IUSRS in COM Security and to save the registry changes as a .reg file into the web role's bin folder).
Copy and paste the out-of-process COM component (OleAutomationFeasibilityModel.exe file) into the bin folder
Create a RegisterOleAutomationFeasibilityModel.cmd file in the bin folder to register the COM component and set the necessary permissions to launch it...
chcp 1252>NUL
OleAutomationFeasibilityModel.exe /regserver
regedit.exe /s SetDCOMPermission.reg
exit /b 0
In the ServiceDefinition.csdef file, insert a reference to the .cmd file immediately before the closing Startup tag...
<Task commandLine="RegisterOleAutomationFeasibilityModel.cmd" executionContext="elevated" />
Publish the web role
Hope that helps someone in a similar situation!
I have placed my own php file in /sites/all/modules/<myfolder> and call it via $.post(<myphppage>) from a static html page (javascript function) also in /sites/all/modules/<myfolder>.
However, the php does not appear to be executing as expected, but is getting called (access logs in Apache HTTPD show it is post'ed to).
If I try to manually request the same php page, I receive this error:
Fatal error: Call to undefined function db_insert() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
The echo statement I have in the php page is outputted properly above this error, and simply uses $_GET['paramname']. (And _POST, changed for testing direct request) to print a few query string parameters for debugging).
Also, when I added a call to watchdog, I receive:
Fatal error: Call to undefined function watchdog() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
Is it not possible to access the PHP page directly? Or is there a Drupal library I need to import? Or am I just plain missing something else with how Drupal works?
You are getting those errors about undefined functions because your PHP file is independent from Drupal and it is not bootstrapping it.
The Drupal way of doing what you are trying to do is to create a module (see Creating Drupal 7.x modules, and Creating Drupal 6.x modules) that defines a URL it handles with hook_menu(); that URL is then used with $.post().
As example of the way of bootstrapping Drupal, see the content of the index.php file that comes with every Drupal installation. (The following code is the one used from Drupal 7.)
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The code changes from Drupal 6 to Drupal 7, but drupal_bootstrap() is defined in both Drupal versions.
The correct way is to create a module, anyway. For a list of reasons why a module should use a PHP file that bootstraps Drupal, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php?