Skip to content

Libraries and frameworks

PHP cURL proxy setup

This guide shows how to use a ProxyOnly proxy with PHP cURL. PHP's cURL extension is the standard way to make HTTP requests through a proxy. You will copy four values from your dashboard (host, port, username and password), add them to PHP cURL, and confirm that the exit address and country are the ones you ordered.

You need an active ProxyOnly proxy first. Create an account, add funds from US$1, and order a proxy in one of the 63 countries we stock. The dashboard then shows the host, port, username and password. If you have not done that yet, start with the quick-start guides.

Tool
PHP cURL
Category
Libraries and frameworks
Proxy protocol
HTTP with username and password
Example host
proxy.proxyonly.com
Countries in stock
63

What you need from your dashboard

Every ProxyOnly proxy has the same four values. The example host and port below are placeholders, so always copy the real ones from your dashboard. The username can carry options after the account name, which is how you choose the country and the session.

SettingExampleWhat it is
Hostproxy.proxyonly.comThe gateway address
Port8000The port for your proxy type, shown in the dashboard
Usernameusername-country-usYour account name, with the country added as -country-xx
PasswordpasswordThe proxy password. Treat it like any other secret
ProtocolHTTPUse an http:// proxy URL, even for HTTPS sites

Set up PHP cURL with a ProxyOnly proxy

PHP cURL is a library. The example below is complete and runs as it is once you replace the host, port, username and password with your own values.

  1. Make sure PHP cURL is installed and up to date.
  2. Build the proxy URL from your dashboard values: http://USERNAME:PASSWORD@HOST:PORT.
  3. Pass the proxy to PHP cURL as shown in the example.
  4. Request an IP echo page such as https://httpbin.org/ip and read the address it returns.
  5. Add a timeout and a retry limit before you run a larger job.
<?php
$ch = curl_init("https://httpbin.org/ip");
curl_setopt($ch, CURLOPT_PROXY, "proxy.proxyonly.com:8000");
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username-country-us:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

echo curl_exec($ch);
curl_close($ch);

Choose the country and the session

You choose the exit country by adding it to the username, and you can pin an address for a while by adding a session name. The same patterns work in PHP cURL because they are part of the credentials.

Use username-country-us for a United States exit, username-country-de for Germany, and username-country-gb for the United Kingdom. Add -session-NAME, for example username-country-us-session-qa42, to keep the same exit for the session. Use a new name to get a new exit. Codes are two-letter ISO country codes, and a country that is not in stock is rejected.

username-country-us
username-country-de
username-country-us-session-qa42

Check that PHP cURL is really using the proxy

Do not assume. Verify the connection before your real job starts, and again after any change to the settings.

  1. Request https://httpbin.org/ip or any IP lookup page with PHP cURL.
  2. Compare the address with your own. It must be different.
  3. Check the country in a lookup tool and compare it with the country in your username.
  4. Run the ProxyOnly proxy tester for the same country as a second opinion.
  5. Repeat the request once. For a sticky session the address should stay the same, and for a rotating one it may change.

Fixing common PHP cURL proxy problems

These are the problems we see most often, with the fix that usually works.

  • 407 Proxy Authentication Required. The username or password is wrong, or the tool did not send them. Copy both from the dashboard again and check the username format.
  • Connection timed out. The host or port is wrong, or a firewall blocks the port. Try curl first to separate a tool problem from a network problem.
  • The exit country is not the one you expected. Check the country code in the username, and look the address up in two lookup tools, because databases do not always agree.
  • TLS or certificate errors on HTTPS pages. Use an HTTP proxy URL (http://...) for the proxy itself, even when the website is HTTPS. The proxy opens a tunnel and does not inspect the traffic.
  • Pages load slowly. Lower your concurrency, reuse connections and try a country closer to the target.
  • The site blocks you. Slow down, send realistic headers, and consider an ISP proxy if the site is strict about datacenter ranges.
  • Your balance ran out. Check the dashboard. Orders are paid from your balance, and an expired rental stops working.

Good habits when you use PHP cURL with a proxy

A proxy only changes the address your traffic comes from. These habits make the difference between a setup that lasts and one that does not.

  • Set a timeout on every request. A request without a timeout can hang for minutes and block a whole worker.
  • Retry with backoff, not in a tight loop. Wait a little longer after each failure and stop after a few attempts.
  • Limit concurrency per target site. A handful of parallel requests is usually enough, and hundreds from one address is how blocks start.
  • Send realistic headers and respect robots rules and rate limits on the sites you read.
  • Log the exit country and the status code of every request so you can see problems early.

Always set CURLOPT_TIMEOUT. Without it a stalled proxy connection can hang a PHP worker.

Using proxies responsibly in any country

A proxy is a networking tool and it is lawful in most places, but what you do with it still has to be lawful and fair. Follow the laws that apply to you and to the place you send traffic to, read the terms of the sites you visit, honour robots rules and rate limits, and never use an address to access accounts or data that are not yours. ProxyOnly cannot give legal advice, and our acceptable use policy applies to every order.

In practice that means reading public pages, checking your own sites and ads, and testing your own products. If a task would be unwelcome to the site owner when done by hand, it is unwelcome when done through a proxy too.

Frequently asked questions

Does PHP cURL work with ProxyOnly proxies?

Yes. ProxyOnly proxies are standard HTTP proxies with username and password authentication, and PHP cURL supports that kind of proxy. Use the setup above and verify the address with an IP lookup.

What proxy settings do I enter in PHP cURL?

Enter the host, the port, your username and your password from the ProxyOnly dashboard. Use HTTP as the proxy type. Add -country-xx to the username to choose the exit country.

Why does PHP cURL show a 407 error?

A 407 means the proxy did not accept the credentials. Copy the username and password again, check for extra spaces, and make sure the username format is correct. Also check that your rental has not expired and that your balance covers the order.

Can I choose the country in PHP cURL?

Yes, by adding the country to the username, for example username-country-de. Check the exit country with an IP lookup, since a country that is not in stock is rejected.

How do I keep the same IP in PHP cURL?

Add a session name to the username, for example username-country-us-session-qa42, and reuse it. A new session name gives you a new exit. Datacenter and ISP proxies you rent also keep the same address for the whole rental period.

Is it safe to put my proxy password in PHP cURL?

It is as safe as any other password in that tool. Do not share exported settings or projects that contain it, do not commit it to a repository and tell support right away if it leaks.

Ready to try it?

Create an account, add funds from US$1 and pick your country and proxy type.