Installing Guzzle
First, we will learn how to install Guzzle using composer. If you don't have composer installed, you can follow the below instructions to install it.
Step 1: Install Composer
Download the composer installer using the following command:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Next, we will have to run the installer using the below command:
Lastly, to make Composer globally accessible from any directory in your terminal, move its binary to the globally accessible path for binaries (i.e., /usr/local/bin/):
sudo mv composer.phar /usr/local/bin/composer
On Windows, navigate to “Advanced System Settings” and select “Environment Variables”. Adding a new path entry to the composer.phar in the PATH Environment variable should serve a similar purpose.
Step 2: Install Guzzle
Now that we have composer installed, we can use it to install Guzzle in our PHP project. We can simply run the following command:
php composer require guzzlehttp/guzzle
Once we execute the above command, it will start installing the guzzle and its dependencies to the current working directory.
Integrating proxies
In this section, we will learn how to integrate proxies with Guzzle.
Step 1: First, let’s create a new PHP file and import the freshly installed library:
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
Step 2: Now, we can make a GET request by creating a client object like the one below:
$client = new Client();
$client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://username:password@<proxy_address>:<port>']);
As you can see, we are initiating a Client object and then using it to send a GET request to the https://ip.oxylabs.io website. We are also passing proxy as an extra argument.
Note: In the proxy URL, we are passing username, password, IP address, and port. You will have to replace these with your Oxylabs proxy user's credentials. If the IP address is public and doesn’t require any authentication, then we can omit the username and password and send the request as below:
$client = new Client();
$client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://<proxy_address>:<port>']);
Rotating proxies
It's also possible to use rotating proxies with Guzzle. We can do this in various ways. For example, if you have a list of IP addresses, you can simply create an array of IPs and rotate them manually using PHP programming. This can become tedious if you have to rotate a huge list of IPs.
Alternatively, you can take advantage of Oxylabs’ solutions, which automatically handle all the rotations and proxy management. In the next few sections, we will see how we can integrate Oxylabs proxies with Guzzle.
Residential Proxies
Type: HTTP, HTTPS, or SOCKS5
Host: pr.oxylabs.io
Port: 7777
$client = new Client();
echo($client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://username:password@pr.oxylabs.io:7777'])->getBody());
You can also use country-specific entries. For example, entering ie-pr.oxylabs.io under Host and 25000 under Port will acquire an Irish exit node. Please refer to our documentation for a complete list of country-specific entry nodes or if you need a sticky session.
We are grabbing the Body of the response and printing it using echo. If everything works correctly, you should see an IP address in the terminal (or in the browser - depending on how you run your PHP script) as soon as you execute the above code.
Enterprise Dedicated Datacenter Proxies
Specify the following if you purchased Dedicated Datacenter Proxies via sales.
Type: HTTP or SOCKS5
IP: a specific IP address (e.g., 1.2.3.4)
Port: 60000
$client = new Client();
$r = $client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://username:password@192.168.2.100:60000']);
echo($r->getBody());
In the above code, we're using an example IP address 192.168.2.100. You will have to replace it with your Dedicated Datacenter IP by choosing an address from the acquired list. Visit our documentation for more details.
Self-Service Dedicated Datacenter Proxies
Specify the following if you purchased Dedicated Datacenter Proxies via the dashboard.
Type: HTTP, HTTPS, or SOCKS5
Host: ddc.oxylabs.io
Port: 8001
For Self-Service Dedicated Datacenter Proxies, the port indicates the sequential number of an IP address from the acquired list. Check our documentation for more details.
Datacenter Proxies
Type: HTTP, HTTPS, SOCKS5
Host: dc.oxylabs.io
Port: 8001
$client = new Client();
$r = $client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://username:password@dc.oxylabs.io:8001']);
echo($r->getBody());
In the pay-per-IP subscription plan, each port corresponds to an IP address in sequence from your list. For example, port 8001 will use the first IP address on the list. For more details, please review our documentation.
For the pay-per-traffic subscription, port 8001 will randomly select an IP address but will keep it consistent during the session. To specify the proxy's location, for example the United States location, include the two letter country code in the user authentication string, like this: user-USERNAME-country-US:PASSWORD. Refer to our documentation for additional details.
ISP Proxies
Type: HTTP, HTTPS, or SOCKS5
Host: isp.oxylabs.io
Port: 8001
Web Unblocker
Integrating Oxylabs’ Web Unblocker with Guzzle is simple and straightforward. Use the following details to make requests:
Type: HTTP or HTTPS
Host: unblock.oxylabs.io
Port: 60000
Here's how the code should look. Notice that the SSL certificate is being ignored:
$client = new Client();
echo($client->request('GET', 'http://ip.oxylabs.io/headers', ['proxy' => 'http://<username>:<password>@unblock.oxylabs.io:60000', 'verify' => false,])->getBody());
Web Unblocker offers multiple features for successful web unblocking, including JavaScript rendering, browser actions, built-in proxy servers, and much more. Take a look at the documentation for a detailed overview of available functionalities.
Oxylabs whitelisted IP proxy setup
In addition to the above methods, Oxylabs also supports whitelisting IP addresses for specific services. Once you whitelist your IP from the Oxylabs dashboard, you can use proxies without sending credentials on each request. This not only simplifies the setup but also increases the security of your proxies since you don’t have to keep your credentials in your scripts anymore.
For example, let’s say you have whitelisted your IP address for using the Oxylabss Residential Proxies. The code that we have written earlier can be simplified and written as below:
$client = new Client();
echo($client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://pr.oxylabs.io:7777'])->getBody());
Oxylabs will automatically detect the IP address from your request and match it with the whitelisted IP and let your request pass through. The same technique can be used with Datacenter Proxies as well. Once whitelisted, you can omit the username and password for those two as well.
So for Dedicated Datacenter Proxies, the code will look like this:
$client = new Client();
echo($client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://192.168.2.100:65432'])->getBody());
And, for Shared Datacenter Proxies, it will be:
$client = new Client();
$r = $client->request('GET', 'https://ip.oxylabs.io', ['proxy' => 'http://dc.pr.oxylabs.io:10000']);
echo($r->getBody());
Conclusion
As you can see, setting up proxies in Guzzle is quite easy and worth all the benefits you get, such as no-hassle HTTP requests sending and more. To learn more about web scraping with PHP, check out this page.
Please be aware that this is a third-party tool not owned or controlled by Oxylabs. Each third-party provider is responsible for its own software and services. Consequently, Oxylabs will have no liability or responsibility to you regarding those services. Please carefully review the third party's policies and practices and/or conduct due diligence before accessing or using third-party services.