Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want use GuzzleHttp with Socks5 proxy

What i want to do

I want use GuzzleHttp with Socks5 proxy.

The problem

It's not working on Linux.
It's successfully workin on Windows.
(Same progam to Linux)

$ php index.php 
PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: Connection refused for URI https://api.ipify.org in /root/test/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php:311
Stack trace:
#0 /root/test/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(230): GuzzleHttp\Handler\StreamHandler->GuzzleHttp\Handler\{closure}()
#1 /root/test/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(322): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure))
#2 /root/test/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(58): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array)
#3 /root/test/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#4 /root/test/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#5 /root/test/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\Middleware::Gu in /root/test/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php on line 72

Program

composer require guzzlehttp/guzzle

composer.json

{
  "require": {
    "guzzlehttp/guzzle": "^7.2"
  }
}

index.php

<?php
require('vendor/autoload.php');

$client = new \GuzzleHttp\Client();
$url = 'https://api.ipify.org';

$response = $client->get($url, [
'proxy' => "socks5://10.0.0.2:8080",
'timeout' => 60, // 60 second
'verify' => false
]);

var_dump($response->getBody()->getContents());

What i try 1

'proxy' => "socks5h://10.0.0.2:8080"

No effect to result.
Ref: https://github.com/guzzle/guzzle/issues/1484#issuecomment-306724234

What i try 2

'proxy' => "tcp://10.0.0.2:8080"

No effect to result.
Ref: https://github.com/guzzle/guzzle/issues/1484#issue-157978804

Tested Environment(Proxy Server)

https://pypi.org/project/pproxy/

$ pproxy -l socks5://0.0.0.0:8080

Tested Environment(PHP)

  • Ubuntu 20.04.1 LTS + php 7.3.24-3 Not working
  • Ubuntu 20.04.1 LTS(WSL) + php 7.4.3Not woring
  • Windows 10 1903 64bit + php 7.2.12 Successfully working
like image 327
bugggy Avatar asked Oct 15 '25 02:10

bugggy


1 Answers

How to solve

Just install php-curl

apt install php7.3-curl

Before

$ php -i | grep cURL
(No output)

After

$ php -i | grep cURL
cURL support => enabled
cURL Information => 7.68.0

Run program on Linux

Successfully working

$ php index.php 
string(15) "xxx.xxx.xxx.x"
like image 148
bugggy Avatar answered Oct 17 '25 17:10

bugggy