Call to undefined function readline()? - php

I am integrating Dropbox into my PHP based website. When i try to run the following code. i got this Fatal error: Call to undefined function readline() on the last line.
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app-info.json");
echo "<pre>";
print_r($appInfo);
echo "</pre>";
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
echo "<pre>";
print_r($webAuth);
echo "</pre>";
$authorizeUrl = $webAuth->start();
echo "1. Go to: " . $authorizeUrl . "\n<br>";
echo "2. Click \"Allow\" (you might have to log in first).\n<br>";
echo "3. Copy the authorization code.\n<br>";
$authCode = \trim(\readline("Enter the authorization code here: "));
I have come through different forum where people said it will work in Command line , But I don't understand how? Any idea ?

Or just use this to simulate it
if(!function_exists("readline")) {
function readline($prompt = null){
if($prompt){
echo $prompt;
}
$fp = fopen("php://stdin","r");
$line = rtrim(fgets($fp, 1024));
return $line;
}
}

readline() is for running on the command line not via the web browser.
To check you have it installed on your server, type:
php -i | grep Configure
Probably, you have not installed it and you should compile it yourself or ask your hosting admin to if they allow this.

Just had this issue on Ubuntu.
Found this answer: https://askubuntu.com/a/264329/166
The solution in this scenario was to install the package php5-readline.

Related

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");

Phpseclib on error execute different command

I am using Phpseclib 1.0 to execute multiple commands on remote server . I know how configure it to exit on error but is there any way to setup that if there is error it will stop and execute different commands ? For example I have 8 commands to execute when it all works it's don't show any output so it's all good . if for example after 4th command it gets error I need that it stop to execute the rest commands and execute command to delete what it did as well another command to run another script to notify admins about accident. The current code I using is
<?php
include('Net/SSH2.php');
ini_set('display_errors', 1);
error_reporting(E_ALL);
$ssh = new Net_SSH2('host');
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
echo '<pre>' . $ssh->exec('create vlan “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST tag 666') . '</pre>';
echo '<pre>' . $ssh->exec('configure vlan TEST add ports 8 untagged') . '</pre>';
echo '<pre>' . $ssh->exec('create l2vpn vpws TEST fec-id-type pseudo-wire 666') . '</pre>';
// Execute on error
echo '<pre>' . $ssh->exec('delete l2vpn vpws “TEST”') . '</pre>';
echo '<pre>' . $ssh->exec('delete vlan “TEST”') . '</pre>';
?>
You could check stderr by doing $ssh->getStdError() and seeing if it's empty. There's also $ssh->getExitStatus().

Working Python script throw out error when PHP calls it with exec

I have a simple php code:
<?php echo exec('/opt/anaconda2/bin/python test2.py 2>&1'); ?><br>
And test2.py simply import a library called theano
import theano
It works under ssh but throw out:
KeyError: 'PATH'
when looking at the php in browser.
What's happeneing here? Is there any way that I can see full trace of error msg?
You can try this for the PHP side of things, but I think KeyError is a Python thing:
<?php
$output = array();
exec('/opt/anaconda2/bin/python test2.py 2>&1', $output, $returnCode);
echo 'Output is: ' . PHP_EOL;
var_dump($output);
echo 'Return code is: ' . PHP_EOL;
var_dump($returnCode);
?>

fuse python script not running in background using php

i am trying to run a file sytem for dropbox ff4d ( from github) in background using php
the purpose if that user will get his dropbox files mount on the server and then i will give the path to a web based explorer (like eXtplorer) so user can manage his file
the script is working fine when using command shell
but when i using the exec function it working printing out the last line of the command shell
and that it . i can not get the folder mount
here the code in php :
$folder = $_POST['foldername'];
$oldumask = umask(0000);
$path = "/var/www/cloudsite/" . $folder;
if(mkdir($path, 0777, true)) {
echo $path . " success directory created ";
} else {
echo $path . "error directory not created ";
}
umask($oldumask);
#$command = '/usr/bin/python /var/www/cloudsite/ff4d/./ff4d.py '. $path .'c7ZYof8Bl0YAAAAAAAAAARThZwAUexbukmv3rMEprPJWFcoQSKGWtWHQBYM40OgC';
$result = exec($command);
if ($result){
echo " </br> execution success";
}else{
echo "</br> error not success";
}
echo $result;
and here what i get in the browser it seems like it working but just hang here nothing mount in the created directory :
var/www/cloudsite/chao success directory created
execution successStarting FUSE...
Within the latest release of my ff4d.py script I've added a "background" switch (-bg).
https://github.com/realriot/ff4d
BTW: Please remove your access key (and revoke it afterwards) because it holds your personal information and ALL your data...

PHP HTTP-Request

I have MAMP Pro installed running php 5.2.13. When I try to initialize a HTTP-Request
$r = new HttpRequest('http://example.com/', HttpRequest::METH_GET);
it tells me:
"Class 'HttpRequest' not found in ...".
What do I need to do to 'install(?)' it?
You must enable http extension:
http://www.php.net/manual/en/http.setup.php
Or you can try new HTTP_Request2:
sudo pear install --alldeps HTTP_Request2-alpha
And then:
$req = new HTTP_Request2('your.url');
$req->setMethod('POST');
$req->setHeader("content-type", $mimeType);
$req->setBody('');
$response = $req->send();
Contemporary Answer for MAMP 2.0 and HTTP_Request2:
Go into your MAMP/bin/php/php5.3.6/bin/ and run
./pear install --alldeps HTTP_Request2
Restart your server and test with the following code, from the PEAR repository:
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2('http://pear.php.net/', HTTP_Request2::METHOD_GET);
try {
$response = $request->send();
if (200 == $response->getStatus()) {
echo $response->getBody();
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Don't forget the require_once statement!
You need to enable the extension ...
add the following to your php.ini
extension = php_http.dll
Apparently that was asked a lot:
http://php.bigresource.com/Track/php-33sNme7A/

Categories