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
Related
I created a new Symfony 4 project via symfony new my_project_name.
Currently when I execute ./bin/console, the output shows
I will create some custom console commands and I want show only my custom commands when I do ./bin/console
Maybe I should create a custom executable 'console' from scratch, but I don't know how do that.
You are creating a complete Symfony application, so all the commands provided by the included packages are available.
Instead of starting from a framework and trying to trim down the parts you do not want, you need to start from further down to have a really barebones project.
Bootstrapping the project:
First, do not use symfony command, no need. Plain old composer will do the trick.
On an empty directory, execute:
composer require symfony/console
This will import the only dependency needed for a console project, and do the basic bootstrapping for your autoloader.
In composer.json, add the following:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
Command class
You'll need one or more commands to actually add to your application. Let's start with a fully fledged greeting application. Create the file src/Greet.php within your project:
declare(strict_types=1);
namespace App;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Greet extends Symfony\Component\Console\Command\Command
{
protected function configure()
{
$this->addArgument('person', InputArgument::OPTIONAL, 'Name of the Entity being greeted', 'World');
$this->addOption('greeting', 'g', InputOption::VALUE_OPTIONAL, 'How to greet the entity', 'Hello');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$greeting = $input->getOption('greeting');
$entity = $input->getArgument('person');
$output->writeln("<info>$greeting $entity!</info>");
}
}
This is a basic command that will print "Hello World!" if executed without any option or argument, will accept one argument use instead of "World", and one option to set the greeting instead of "Hello".
Console Application
On the project's root, create a file app.php.
require __DIR__ . '/vendor/autoload.php';
$app = new Symfony\Component\Console\Application('Hello World App', '1.0.0');
$app->add((new App\Greet('greet')));
$app->run();
This is be a very short script, so let's go line by line
Require the autoloader. This script is the entry point for the application, so we need to execute the autoloader.
Instantiate the application. This creates a new instance of the Symfony Console application, sets a name and version for the app. This will be used in the "help" printouts for the app.
Add the command. By default, the application has no commands at all. We'll need to add the command we have just created. add() expects a Command instance. We instantiate the command we just created, and we set it to be called by the name "greet".
Run the application
Generate autoloader.
Execute composer dump-autoload so the autoloader for your application is generated.
Execute the script.
If you now execute php app.php you'll get:
Hello World App 1.0.0
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
greet
help Displays help for a command
list Lists commands
Note that the only available command is greet. Which you can use like this:
# php app.php greet
Hello World!
# php app.php greet Alice
Hello Alice!
# php app.php greet Bob -g "Good morning"
Good morning Bob!
# php app.php help greet
Usage:
greet [options] [--] [<person>]
Arguments:
person Name of the Entity being greeted [default: "World"]
Options:
-g, --greeting[=GREETING] How to greet the entity [default: "Hello"]
[... default generic options omitted]
I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap
I created a index.php file with
<?php
require __DIR__ . '/vendor/autoload.php';
$hosts = Nmap::create()->scan([ 'example.com' ]);
$ports = $hosts->getOpenPorts();
echo $ports;
?>
This is what my composer.json file
{
"require": {
"willdurand/nmap": "^0.5.0"
}
}
When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.
When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.
You have to either add the namespace to the class defenition:
$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);
Or, add a using statement for this class at the top of your file: (under <?php ofcourse)
use Nmap\Nmap;
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!
This is a strange error that's constantly occurring.
Fatal error: Class 'Guzzle\Http\Client' not found in /home/futcoins/public_html/autobuyer/classes/shopify.php on line 15
This is the source code. I think this question is pretty straight forward and I've been stuck with this problem for a couple of days, any ideas?
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\FileCookieJar;
class Shopify {
//initialise the class
public function __construct() {
}
public function GetOrders() {
$client = new Client(null); //Line 15 where errors occurs
$request = $client->get("url");
$response = $request->send();
$json = $response->json();
return $json;
}
}
So you have a declaration at the top
use Guzzle\Http\Client;
That means you either have an autoloader or have included the appropriate file(s) manually. So you need to find the file that has that class and include it or else PHP will be looking for code you've not given to it.
I'm not sure it is the right solution for you but I had the exact same problem and to fix it I updated composer on my server and regenerated the autoload file:
sudo /usr/bin/composer.phar self-update
/usr/bin/composer.phar dump-autoload
I'm not sure it is necessary but I also restarted apache:
sudo /etc/init.d/httpd restart
To prevent this in the future and because we are using Elastic Beanstalk I created an configuration file to make sure that composer is up to date:
commands:
01updateComposer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
Source: http://blogs.aws.amazon.com/php/post/Tx2M04LCN1UEE0E/Reduce-Composer-Issues-on-Elastic-Beanstalk
I added to my project FOSUserBundle, on localhost it's works fine. But on web server I get
Fatal error: Class 'FOS\UserBundle\FOSUserBundle' not found in
/home/zone24/domains/zone24.linuxpl.info/public_html/worldclock/app/AppKernel.php on line 22
I can't cache:clear because I get this same message.
My autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The line from AppKernel.php who make mistake
new FOS\UserBundle\FOSUserBundle(),
Folderfriendsofsymfony in /vendor has 775 permisions
Are you using APC ? If yes, restart apache to clear its cache.
If that does not help, you can always force the autoloader to register a specific namespace with $loader->add(), but you should not have to do that. FOS works fine for me without adding that.