PHP Selenium Blocked by CORS Policy - php

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!

Related

Class not found exception in PHP from composer

I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.

Error GRPC Spanner Google Cloud With PHP

I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini

Twilio token not found when doing the QuickStart

Context: I am following the instructions provided in the Twilio iOS Quickstart to test out the Chat function. A simple PHP server is required to generate tokens.
Inside token.php, these are the lines to generate the token:
$token = new AccessToken(
$TWILIO_ACCOUNT_SID,
$TWILIO_API_KEY,
$TWILIO_API_SECRET,
3600,
$identity
);
The instructions only asked me to modify config.php, which I have:
<?php
// Required for all uses
$TWILIO_ACCOUNT_SID = 'actual_SID';
$TWILIO_API_KEY = 'actual_API_key';
$TWILIO_API_SECRET = 'actual_API_secret';
// Required for Video
$TWILIO_CONFIGURATION_SID = '';
// Required for Chat
$TWILIO_CHAT_SERVICE_SID = 'actual_Chat_Service_SID';
// Required for Notify
$TWILIO_NOTIFICATION_SERVICE_SID = '';
// Required for Sync
$TWILIO_SYNC_SERVICE_SID = '';
As they have instructed, I run the server with php -S localhost:8000 -t webroot and get the following error:
/token.php?device=browser - Class 'Twilio\Jwt\AccessToken' not found in /Users/me/Downloads/sdk-starter-php-master/webroot/token.php on line 19
Twilio developer evangelist here.
Before running the application with php -S localhost:8000 -t webroot make sure you have installed the dependencies using composer:
composer install
Then you should be ready to run.

Class 'Proxy\Factory' not found PHP error although file exists

I downloaded this lib, extracted it and uploaded it to my php webspace: https://github.com/jenssegers/php-proxy
Also, I added a new index.php file with this content from the example included to the lib archive:
<?php
use Proxy\Factory;
use Proxy\Response\Filter\RemoveEncodingFilter;
use Symfony\Component\HttpFoundation\Request;
require 'vendor/autoload.php';
// Create the proxy factory.
$proxy = Factory::create();
// Add a response filter that removes the encoding headers.
$proxy->addResponseFilter(new RemoveEncodingFilter());
// Create a Symfony request based on the current browser request.
$request = Request::createFromGlobals();
// Forward the request and get the response.
$response = $proxy->forward($request)->to('http://example.com');
// Output response to the browser.
$response->send();
?>
The file structure looks like this (collapsed):
Expanded:
But when I run the script, I get this error:
Fatal error: Class 'Proxy\Factory' not found in index.php on line 10
Line 12 would be this:
$proxy = Factory::create();
Why does this happen? Shouldn't use Proxy\Factory; properly include the file Factory.php?
Update: The content of autoload.php is this:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit60f173139b462a85925faf1b40913ce6::getLoader();
I finally got it to work. I had totally messed up the Symfony project structure, so here is what I did to get it to work:
Run in shell (on Mac OS X):
$ symfony new proxy 2.7
$ cd proxy
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require jenssegers/proxy
$ sudo cp /etc/php.ini.default /etc/php.ini
In /etc/php.ini search for timezone and remove the colon in front. Then set date.timezone = "Europe/Berlin".
Then I edit this file: src/AppBundle/Controller/DefaultController.php and add my code:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Proxy\Factory;
use Proxy\Response\Filter\RemoveEncodingFilter;
require '/Users/me/dev/symphony/proxy/vendor/autoload.php';
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
/*
return $this->render('default/index.html.twig', array(
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
));
*/
// Create the proxy factory.
$proxy = Factory::create();
// Add a response filter that removes the encoding headers.
$proxy->addResponseFilter(new RemoveEncodingFilter());
// Create a Symfony request based on the current browser request.
$request = Request::createFromGlobals();
// Forward the request and get the response.
$response = $proxy->forward($request)->to('http://example.com');
// Output response to the browser.
$response->send();
}
}
Then just run the server:
$ php app/console server:run
And open localhost:8000.
Try moving index.php one level up in directory structure: require vendor/autoload.php inside vendor dir looks too strange, it is not very likely you (and anyone) would have vendor directory inside vendor directory

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