Search code examples
phpcurlphantomjsphp-phantomjs

Using proxies in PhantomJS with PHP PhantomJS library


The documentation for PhantomJS does show how to use proxies. However, how is is used in PHP when using the library from PHP PhantomJS?

For that matter, how are any of the PhantomJS addons used?

I'm currently doing this with CURL to use proxies:

curl_setopt($curl, CURLOPT_PROXY, "http://$proxy:$port");
curl_setopt($curl, CURLOPT_PROXYUSERPWD, "$username:$password");

I'd like to do the exact same thing with PhantomJS. I have it installed and configured properly and this example works (PHP PantomJS's own example).

use JonnyW\PhantomJs\Client;

$client = Client::getInstance();
$request  = $client->getMessageFactory()->createRequest();
$response = $client->getMessageFactory()->createResponse();
$request->setMethod('GET');
$request->setUrl('http://jonnyw.me');

$client->send($request, $response);
print_r($response);

Where does the proxy info go here?

Thanks. I'm very new to PhantomJS.


Solution

  • From the official PHP PhantomJS documentation, section "PhantomJS options". You can add options for running PhantomJS binary like this:

    <?php
    
        use JonnyW\PhantomJs\Client;
    
        $client = Client::getInstance();
        $client->getEngine()->addOption('--load-images=true');
        $client->getEngine()->addOption('--ignore-ssl-errors=true');
    

    So you would add proxy information like this:

        $client->getEngine()->addOption("--proxy=$proxy:$port");
        $client->getEngine()->addOption("--proxy-auth=$username:$password");
    

    There is also an option for proxy type (http|socks5|none):

        $client->getEngine()->addOption("--proxy-type=socks5");