Execute PHP script from python - php

I try to run my php script from python.
Here is my php code:
<?php
require '../functions/server_info.php';
require ('../functions/functions.php');
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$input_data_trunk = array();
$input_data_trunk['ami_category']="TRK-IPU";
$IP = "192.168.1.200";
conf_sip_action($input_data_trunk, 'new', 'trunk');
echo "done";
?>
Then I write python code i execute this script, but I have error like this:`
PHP Warning: require(../functions/server_info.php): failed to open stream: No such file or directory in /var/www/html/IPU-GUI/website2/LTU_trunk.php on line 2
PHP Fatal error: require(): Failed opening required '../functions/server_info.php' (include_path='.:/usr/share/php') in /var/www/html/IPU-GUI/website2/LTU_trunk.php on line 2
Here is my python code (just execute script):
import subprocess
subprocess.call( ["/usr/bin/php", "/var/www/html/IPU-GUI/website2/LTU_trunk.php"] )
I am looking for help.
Thanks in advance.
`

require .. is relative to the current working directory/the PATH setting, not the current PHP file. When executing from Python, the working directory will be either undefined or simply different from what you might expect. Use an absolute import path/relative to the file:
require dirname(__DIR__) . '/functions/server_info.php';

Related

PHP set_include_path and require_once

I have an error that happens only on my host server, on my local environment not:
Fatal error: require_once() [function.require]: Failed opening required '\Audero\Loader\AutoLoader.php' (include_path='.:/usr/share/pear:/usr/share/php:/home/httpd/vhosts/webox-it.com/ofrom.webox-it.com/modules/Concordancier....\lib') in /home/httpd/vhosts/webox-it.com/ofrom.webox-it.com/modules/Concordancier/data_manager.php on line 1670
Here is my code:
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../../lib/');
require_once 'Audero\Loader\AutoLoader.php';
spl_autoload_register('Audero\Loader\AutoLoader::autoload');
// Extract the chunk and save it on the hard disk
try {
$extractor = new \Audero\WavExtractor\AuderoWavExtractor($inputFile);
$extractor->saveChunk($start, $end, $outputFile);
...
Files structure:
/lib/Audero/Loader/Autoloader.php
Calling file emplacement:
/modules/Concordancier/data_manager.php
In my local environment there's no problem, php can load the autoloader, but in the prod server it does not.
The source files structure is exactly the same as the local one, and the relative path looks correct. The php version in prod is 5.3.27 so it should work ?
Any idea ?

Can't Parse Up One Directory Command in PHPUnit Testing with PHPStorm

I have PHPUnit set up in my PHPStorm project. I've referenced the PHPUnit phar file and have a PHP executable linked to my PHPStorm run configuration. My directory structure looks like:
/lib/classes/Class.php
/lib/vendor/phpunit.phar
/lib/test/ClassTest.php
In my ClassTest.php file, I reference the other two files with:
require_once (__DIR__ . "../vendor/phpunit.phar");
require_once (__DIR__ . "../classes/Class.php");
I get the following error when I run my test:
Fatal error: require_once(): Failed opening required
'C:\Users\me\PhpstormProjects\myproject\lib\tests../vendor/phpunit.phar'
It seems like the PHP parser isn't correctly parsing the up one directory ../ command.
Why is this happening?
First of all: this line is not needed (at all) as PHPUnit will already be loaded at that time.
require_once (__DIR__ . "../vendor/phpunit.phar");
Secondly: __DIR__ constant in PHP does not contain trailing slash. When used in require/include statements (and other places when building full file path) you have to add it yourself.
In your particular case it has to be (note / before ..):
require_once (__DIR__ . "/../classes/Class.php");

Start Solarium Client

I have problem with start Solarium Client, I downloaded it by Composer and GitHub according the wiki tutorial, it's ok, but I don't know what I must do now.
When I tried this example code:
<?php
require(__DIR__.'/init.php');
htmlHeader();
// check solarium version available
echo 'Solarium library version: ' . Solarium\Client::VERSION . ' - ';
// create a client instance
$client = new Solarium\Client($config);
// create a ping query
$ping = $client->createPing();
// execute the ping query
try {
$result = $client->ping($ping);
echo 'Ping query successful';
echo '<br/><pre>';
var_dump($result->getData());
echo '</pre>';
} catch (Solarium\Exception $e) {
echo 'Ping query failed';
}
htmlFooter();
I have the following error:
Warning: require(C:\xampp\htdocs\solarium/init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\solarium\test.php on line 3
Fatal error: require(): Failed opening required 'C:\xampp\htdocs\solarium/init.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\solarium\test.php on line 3
I use Xampp server.
Must I set some Path, where appropriate, how and where? File init.php is in folder: C:\xampp\htdocs\solarium\vendor\solarium\solarium\examples\ and i start the demo file in folder: C:\xampp\htdocs\solarium\
Sorry for my English. Thank you for help.
You have to run solarium examples, and examples derived form them like your test.php file, from the /examples directory in the solarium directory tree.
In this directory there is init.php file which is setting up 'display_errors' init variable, loading composer created autoload.php, loading the config.php and defining htmlHeader() and htmlFooter() functions.

PHP include with ../ failed open

I am trying to include a php file from the parent directory and I getting error:
admin#webby:~$ /usr/bin/php
/var/phpscripts/email_parser/tests/_email-test.php PHP Fatal error:
require_once(): Failed opening required '../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
Fatal error: require_once(): Failed opening required
'../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
PHP file
#!/usr/bin/php
<?php
error_reporting(E_ALL ^ E_NOTICE ^E_WARNING);
ini_set("display_errors" , 1);
require_once("../PlancakeEmailParser.php");
// etc
?>
Folder Structure
admin#webby:/var/phpscripts/email_parser$ find .
.
./composer.json
./README.txt
./LICENSE.txt
./PlancakeEmailParser.php
./tests
./tests/_email-test.php
For testing it works fine when I move PlancakeEmailParser.php into the tests directory and remove the "../" from the require
The line
require_once("../PlancakeEmailParser.php");
Is not using a fully qualified file-system path or PHP-URI-scheme. Therefore its outcome depends on PHP configuration, most often because of the include directory configuration.
The PHP CLI can use a different configuration file than with your webserver - or - the working directory is different as with your webserver (probably the later plays more of a role in your specific scenario).
What you want can be easily expressed fully qualified, too:
require_once(__DIR__ . "/../PlancakeEmailParser.php");
This is probably what you're looking for.
Quote from PHP CLI SAPI: Differences to other SAPIs:
It does not change the working directory to that of the script. (-C
and --no-chdir switches kept for compatibility)
In order to keep relative paths in your script working as-is, change directory to where the script resides, then execute the script:
cd /var/phpscripts/email_parser/tests/ && ./_email-test.php

crontab cannot find required php file when running a php script

I am trying to get crontab to run a php file, here is the cronjob
10 * * * * /usr/bin/php /var/www/update/ranks.php >> /var/www/update/log/ranks.txt
But I keep getting an error saying the required file does not exist
PHP Warning: require_once(../mws_products.php): failed to open stream: No such file or directory in /var/www/update/ranks.php on line 2
PHP Fatal error: require_once(): Failed opening required '../mws_products.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/update/ranks.php on line 2
I do not get this problem when I run the file from a browser or when I go into the directory of the file and execute the file e.g. cd /var/www/update/
ranks.php
<?php
require_once('../mws_products.php');
echo "-------------------------------------------------------------\n";
echo date('d-M-Y H:i:s',time())."\n";
echo "Update Ranks\n";
$products->updateRanks();
$database->addUpdate("ranks", time());
echo "\n\n\n";
?>
folder structure
[folder] update
|____ [file] ranks.php
[file] mws_products.php
What could be causing this problem? (note: I have tried restarting apache and the server)
As your are running in a crontab from the root directory ../mws_pruducts.php does not exists, relative to the given root.
There are multiple solutions, this is one of them:
define( 'ROOT', dirname(__FILE__) );
require_once(ROOT . '/../mws_products.php');
Try with the absolute path for the file...
require_once('/var/www/mws_products.php');

Categories