Stripe API - PHP Curl request behind a proxy

断了今生、忘了曾经 提交于 2021-01-28 06:05:39

问题


I'm trying to make a request behind a proxy to stripe api using php and curl. The following works if I'm not behind a proxy:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/balance");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$a_pass");
$out = curl_exec($ch); # returns my balance
curl_close($ch);

A request to any other domain using the proxy works:

$username = 'user'; 
$password = 'pass'; 
$proxy = 'proxy:port'; 

$ch = curl_init('https://api64.ipify.org?format=json'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password"); 
$result = curl_exec($ch); # returns the proxy ip
curl_close($ch); 

But if I mix both, It doesn't work and a 403 error is returned by stripe api:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/balance");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$a_pass");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password"); 
$out = curl_exec($ch);
if (curl_errno($ch)) {
    print_r($curl_error($ch)); # 403
}
curl_close($ch);

It seems the CURLOPT_USERPWD is lost if the request is made using a proxy.
Any idea how to use a CURLOPT_PROXYUSERPWD, CURLOPT_PROXY and CURLOPT_USERPWD sucessfully on the same request?


Notes:

  • I cannot install any additional software on this machine.

来源:https://stackoverflow.com/questions/65745669/stripe-api-php-curl-request-behind-a-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!