How to install mailgun in PHP? - php

This is how I install mailgun on a VM that runs PHP:
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Add Mailgun as a dependency
php composer.phar require mailgun/mailgun-php:~2.0
# Add Guzzle 6 as a dependency
php composer.phar require php-http/guzzle6-adapter:^1.1.1
But when I load a page with this content:
<?php
require_once('/app/mailgun-php/vendor/autoload.php');
echo 'Current PHP version: ' . phpversion();
ini_set('display_errors' , 'On');
$client = new \Http\Adapter\Guzzle6\Client();
$mailgun = new \Mailgun\Mailgun('123', $client);
# use Mailgun\Mailgun;
?>
I get the following error:
Current PHP version: 5.5.9-1ubuntu4.14
Fatal error: Class 'Http\Adapter\Guzzle6\Client' not found in /app/sign-in.php on line 5
What is wrong with the installation?

You need to include the Composer autoloader so PHP knows where the Mailgun Client class actually is:
<?php
require_once('vendor/autoload.php');
echo 'Current PHP version: ' . phpversion();
ini_set('display_errors' , 'On');
$client = new \Http\Adapter\Guzzle6\Client();
?>

Related

PHP Selenium Blocked by CORS Policy

I am trying to access a website with selenium and i am getting below error
And i used the following code i have tried header('Access-Control-Allow-Origin: *'); but did't work for me
require_once "phpwebdriver/WebDriver.php";
$webdriver = new WebDriver("localhost",4444);
//$ffprofile = $webdriver->prepareBrowserProfile("");
$webdriver->connect("chrome");
$webdriver->get("https://healofy.com/"); sleep(3);
$element=$webdriver->findElementBy(LocatorStrategy::id,"Baby_1_2_years");
if($element) {
print_r($element);
$element->click();
}
It could be you're using old php webdriver client (2013) ? which is not compatible with current selenium and browser.
use updated PHP selenium facebook/webdriver and here the setup step:
# if you have composer
composer require facebook/webdriver
# if not download composer.phar
curl -sS https://getcomposer.org/installer | php
php composer.phar require facebook/webdriver
read the github page above if it missing something.
and PHP code
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get("https://healofy.com/");
$driver->findElement(WebDriverBy::xpath('//label[#for="Baby_1_2_years"]'))->click();
//$driver->quit();
?>
If you use Selenium webdriver and want to get image from page, but blocked by cors.
You can just say Selenium to go to image url and after that take screenshot.
And your image will be saved!
And cors will not be a problem.
Here is example code in PHP:
$img_url = '.../6028384213.jpg';
$driver->get($img_url);
$driver->takeScreenshot('img.png');
Your image will be opened by browser and after that saved to your local computer!

Fatal error: Uncaught Error: Class 'LanguageClient' not found

just using playing around with Google's Natural Language API with php, but I can't seem to run a simple example.
Here is the basic for my php:
<?php
# Imports the Google Cloud client library
use Google\Cloud\Language\LanguageClient;
# Your Google Cloud Platform project ID
$projectId = '<My Project Name>';
# Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId
]);
# The text to analyze
$text = 'Hello, world!';
# Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
?>
But it comes up with this error:
Fatal error: Uncaught Error: Class 'LanguageClient' not found in /User/zan/Zan/classifier/test.php:11
Stack trace:
#0 {main}
thrown in /Users/zan/Zan/classifier/test.php on line 11
I used composer to install google/cloud, but have no clue why it's unable to find LanguageClient. Can anyone point me in the right direction?
Did you include the composer autoloader?
include __DIR__ . "/vendor/autoload.php";
This example assumes you ran composer install in the same directory your code is running. Modify the path accordingly to match your configuration.
just a quick update, so I managed to get it working in the end. Since it was a new laptop, most of my php files weren't setup correctly.
I was missing these components:
autoconf
pecl
php-unit
grpc
protobuf
pcre
Lastly, running a
composer update
within the directory and adding the header
include __DIR__ . "/vendor/autoload.php";
I then ran the script and it worked, thanks to jdp for add and Martin for the heads up.

PHP protobuf error - Undefined method for encode/decode

I'm trying to learn about Protobuf in PHP using https://github.com/google/protobuf/tree/master/php . Currently I'm stuck in an error.
My steps to install protobuf:
Install protobuf through pecl with command:
sudo pecl install protobuf-3.2.0a1
Set composer.json as below, then run sudo composer install
{
"require": {
"google/protobuf": "^3.2"
}
}
Below is my code:
Proto file:
syntax = "proto3";
message APIReq {
string functionName = 1;
string name = 2;
int32 time = 3;
string type = 4;
}
Command to generate PHP Class from .proto file:
protoc --php_out=/var/www/html/ MsgFormat.proto
The protoc command resulted in two file, APIReq.php and GPBMetadata/MsgFormat.php
After that, I added require_once __DIR__ . '/vendor/autoload.php'; and require_once __DIR__ . '/GPBMetadata/MsgFormat.php'; in the generated PHP file because when I ran php APIReq.php it came up with
PHP Fatal error: Class 'Google\Protobuf\Internal\Message' not found in /var/www/html/testing/APIReq.php on line 13
After I added those line, the error disappeared, so I assume both line fixed the problem
my PHP file (following example from https://developers.google.com/protocol-buffers/docs/reference/php-generated, section Messages):
<?php
require __DIR__ . '/vendor/autoload.php';
include_once('APIReq.php');
$param = new APIReq();
$param2 = new APIReq();
$param->setFunctionname('functionname');
$param->setName('name');
$param->setTime(123456);
$param->setType('type');
$dt = $param->encode();
$param2->decode($dt);
?>
When I run the PHP code, it returns error message:
PHP Fatal error: Call to undefined method APIReq::encode()
How can I fix this?
Edit: Tried this with protobuf 3.3.0 as well, with same result.
Encode & Decode not exist in the codebase as I traced down.
This change was introduced in 3.3.0
//to encode message
$data = $param->serializeToString();
//to decode message
$param2 = new APIReq();
$param2->mergeFromString($data);

nodejs cannot find module 'zombie' with PHP mink

I'm trying out Mink (PHP) on Ubuntu 14.04; I basically did the following:
$ apt-show-versions nodejs
nodejs:amd64/trusty 0.10.45-1nodesource1~trusty1 uptodate
$ npm -v
2.15.1
$ sudo npm install -g zombie
npm WARN engine zombie#4.2.1: wanted: {"node":"^4.0.0"} (current: {"node":"0.10.45","npm":"2.15.1"})
...
zombie#4.2.1 /usr/lib/node_modules/zombie
├── ms#0.7.1
├── debug#2.2.0
...
$ ls /usr/lib/node_modules/zombie/node_modules/
babel-runtime bluebird debug eventsource iconv-lite jsdom lodash mime ms request tough-cookie ws
So, basically, even if I get a warning, the modules build, and should be in the directory /usr/lib/node_modules.
Then I do:
mkdir test_php_mink
cd test_php_mink/
composer require behat/mink
composer require behat/mink-zombie-driver
As a check:
test_php_mink$ ls
composer.json composer.lock vendor
... it seems all composer files are there.
Finally, as per http://mink.behat.org/en/latest/drivers/zombie.html (and also Cannot find module 'zombie' · Issue #84 · assaf/zombie · GitHub), I'm trying this script:
<?php
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
echo "safe_mode: '" . ini_get("safe_mode") ."'\n"; # have PHP 5.5.9, safe_mode is removed
putenv("NODE_PATH=/usr/lib/node_modules");
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # OK, is there
# NOPE:
#$driver = new \Behat\Mink\Driver\ZombieDriver();
$driver = new \Behat\Mink\Driver\ZombieDriver(
new \Behat\Mink\Driver\NodeJS\Server\ZombieServer()
);
$session = new \Behat\Mink\Session($driver);
// start the session
$session->start();
?>
This script, unfortunately, still fails with:
$ php test_php_mink.php
safe_mode: ''
NODE_PATH is: '/usr/lib/node_modules'
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (8) [
module.js:340
throw err;
^
Error: Cannot find module 'zombie'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js:3:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
]' in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php:413
Stack trace:
#0 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php(306): Behat\Mink\Driv in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php on line 413
How can I get this basic example to run?
EDIT: Played around a bit more with this, and discovered that when I specify the environment variable on the command line:
$ NODE_PATH=/usr/lib/node_modules php test_php_mink.php
safe_mode: ''
NODE_PATH is: '/usr/lib/node_modules'
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (8) [
/usr/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:238
var nonInheritedTags = new Set([
^
ReferenceError: Set is not defined
...
... then the module seems to be found! So my question basically reduces to: how can I change the NODE_PATH environment variable from my php script, so I wouldn't have to specify it on the shell - since apparently putenv("NODE_PATH=/usr/lib/node_modules"); does not really work for me...
As for the new error, there is Installing Zombie.js Error: ReferenceError: Set is not defined. What am I doing wrong? - apparently this is due to the version mismatch that I got a warning for (npm WARN engine zombie#4.2.1: wanted: {"node":"^4.0.0"} (current: {"node":"0.10.45","npm":"2.15.1"})), so I guess I'll have to install nvm so I can install the right nodejs version; and I also noticed in /usr/lib/node_modules/zombie/README.md:
Zombie 4.x is tested to work with io.js 1.6 or later.
If you need to use Node 0.12 or earlier, consider using Zombie 2.x. ...
To install Zombie.js you will need io.js:
```bash
$ npm install zombie --save-dev
```
... and I think that can also be installed with nvm; so I'll give that a try...
Ok, found some sort of a method which seemingly works - but I'd still like someone more knowledgeable to answer.
Anyways, the trick is - zombie can accept a path to the nodejs binary; so if you cannot really pass environment variables for nodejs from PHP, then make a shell script which will set these environment variables, and then call nodejs.
First this was my install:
# remove previous
sudo npm uninstall -g zombie --save-dev
sudo apt-get remove --purge nodejs && sudo apt-get autoremove --purge
# install new
curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
nvm install iojs-v3.3.1
npm list -g --depth=0
nvm install 4.0.0
npm list -g --depth=0
npm -g install zombie --save-dev
The problem with nvm is that it installs in a user directory, and I'd like to test my scripts both on my user machine and remote server, where my uids are completely different. Regardless, using a custom executable helps a bit with that. So, create a script in a "global" location, I chose /home, so I'll need sudo to create files there:
sudo touch /home/node_pth.sh
... then paste in the following content:
#!/bin/bash
export NODE_PATH=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules
#echo ARGS ARE "$#" | tee -a /tmp/node.log
/home/USERNAME/.nvm/versions/node/v4.0.0/bin/node "$#"
... of course, replacing the paths with your correct ones; then finally make it executable:
sudo chmod +x /home/node_pth.sh
Now we can use the following test_php_mink.php PHP file:
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help
$nodePath = "/home/node_pth.sh"; # shell script that sets NODE_PATH, then calls node executable
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; #
putenv("NODE_PATH=".$nodeModPath);
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # is there - but still doesn't help with call
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
echo "safe_mode: '" . ini_get("safe_mode") ."'\n"; # have PHP 5.5.9, safe_mode is removed
$driver = new \Behat\Mink\Driver\ZombieDriver(
//~ new \Behat\Mink\Driver\NodeJS\Server\ZombieServer()
# copy defaults here for everything but nodeBin;
# see vendor/behat/mink-zombie-driver/src/NodeJS/Server.php
new \Behat\Mink\Driver\NodeJS\Server\ZombieServer("127.0.0.1", 8124, $nodePath, null)
);
$session = new \Behat\Mink\Session($driver);
// start the session
$session->start();
?>
... OR, I just realized there is setNodeModulesPath($nodeModulesPath) in vendor/behat/mink-zombie-driver/src/NodeJS/Server.php, so we can drop the proxy bash executable altogether:
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help via putenv
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; #
putenv("NODE_PATH=".$nodeModPath);
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'\n"; # is there - but still doesn't help with call
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
echo "safe_mode: '" . ini_get("safe_mode") ."'\n"; # have PHP 5.5.9, safe_mode is removed
$zsrv = new \Behat\Mink\Driver\NodeJS\Server\ZombieServer();
$zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
$driver = new \Behat\Mink\Driver\ZombieDriver( $zsrv );
$session = new \Behat\Mink\Session($driver);
// start the session
$session->start();
?>
Anyways, when this script is called, it outputs:
$ php test_php_mink.php
NODE_PATH is: ''
NODE_PATH is: '/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules'
safe_mode: ''
... and as there are no errors, I'm assuming it is all fine now...

Fatal error: Class 'OpenCloud\Rackspace' not found

For a custom application, I am trying to integrate Rackspace cloud files using php-opencloud library.
This is the link I followed for setup -https://github.com/srijanaravali/php-opencloud/blob/master/docs/getting-started.md
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Require php-opencloud as a dependency
php composer.phar require rackspace/php-opencloud:dev-master
However, when I try to instantiate a client object, it throws an error:
Fatal error: Class 'OpenCloud\Rackspace' not found in /var/www/example/Project/sites/all/libraries/php-opencloud/test.php on line 7
Here is the code snippet:
<?php
require 'vendor/autoload.php';
use OpenCloud\Rackspace;
// 1. Instantiate a Rackspace client.
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => getenv('Axxxxxxx'),
'apiKey' => getenv('abcxxxxxxxxxxxxxxxxxxxx')
));
print_r($client); die('!!');
Any pointers about whats missing?
Got it working. For some strange reason, php-opencloud library was empty under vendors/rackspace/php-opencloud.
Cloned one from github and created a symlink to it from above directory. It is working fine now.

Categories