PHP cURL connection refused while using proxy - php

I am getting this error in my PHP cURL
Failed to connect to ip port after 1000 ms: Connection refused
I have this code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PROXY, 'ip');
curl_setopt($curl, CURLOPT_PROXYPORT, 'port');
curl_setopt($curl, CURLOPT_PROXYUSERPWD,'user:pass');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if ($result === FALSE) {
die(curl_error($curl));
}
curl_close($curl);
I don't know why I am having this error.
What seems to be the problem here?

Related

php get error Unknown SSL protocol error in connection

I should connect to a webservice that use Basic Authntication and GET method to fetch a json string. when I enter url in browser I get correct result but when I try it using cUrl I get error bellow:
Unknown SSL protocol error in connection to www.example.com:443
here is code:
$url = "https://www.example.com/api/Voucher/VerifyDiscountCode?discountCode=test&cellPhoneNumber=123456&otp=false";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
//curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, "test:test");
$data = curl_exec($curl);
if($data === false) $data = curl_error($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo $data;
note: it is funny that this code works in my local pc but not in server! I am sure it is not firewall thing. not sure it is from php configuration or what?

How check private proxy with CURL?

I need check proxy with CURL in my script, but there is error "couldn't connect to host".
Please see the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'xx.xxx.xx.103:8080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'login:pass');
$data = curl_exec($ch);
if ($data === false)
{
echo "Proxy is not working: ", curl_error($ch);
}
else
{
echo "OK";
}
Thanks to all who can help!
Remove port after ip and set port in given cURL option below
curl_setopt($ch, CURLOPT_PROXY, 'xx.xxx.xx.103');
curl_setopt($ch, CURLOPT_PROXYPORT, '8080');

curl error: 7 Failed to connect to www.******.com port 80: Connection timed out

I am writing code to get content from other URL curl.
My code is working fine on localhost but when I uploaded on server its not working.
It showing curl error:
curl error 7,Failed to connect to www.******.com port 80: Connection
timed out.
This is my Code
function get_data($url) {
//echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
if(!$data ) {
echo "curl error->".curl_errno($ch)."->".curl_error($ch);
}
curl_close($ch);
return $data;
}

PHP - CURLOPT_PROXYPORT issue

I am so confused about my cURL script requesting the target page "facebook.com" for example, with the same port as the proxy.
example:
proxy is: 178.215.111.70:9999 . a cURL error says:
Failed connect to facebook.com:9999; Connection timed out
I see that it tries to connect to facebook using the poxy's port: 9999
Here is my code:
<?php
$curl = curl_init("https://facebook.com");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_REFERER, 'https://www.facebook.com');
if ($var) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "test");
}
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,9999999);
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
curl_setopt($curl, CURLOPT_PROXY, '178.215.111.70');
curl_setopt($curl, CURLOPT_PROXYPORT, '9999');
$result['exe'] = curl_exec($curl);
$result['err'] = curl_error($curl);
curl_close($curl);
echo $result['err'];
echo $result['exe'];
?>
I solved my problem.
It was a problem in the server only. When i tried the SAME script in another server it worked 100% fine
So always make sure of the configuration in your server when you face a problem like this.

file_get_contents on other port

I must contact services rest on different ports by 80, but the function file_get_contents () returns an error: failed to open stream: Connection refused
$url = "http://nexusdigital.agency:81/API/....";
$result = file_get_contents($url, false);
How can I configure the reading on other ports?
Use CURL :
<?php
$curl = curl_init('http://nexusdigital.agency/API/....');
curl_setopt($curl, CURLOPT_PORT, 81);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 81);
$result = curl_exec($curl);
?>
Well, if it doesn't work for you (whatever the reason) you can try using CURL http://php.net/curl
<?php
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "http://nexusdigital.agency/API/....");
curl_setopt($tuCurl, CURLOPT_PORT , 81);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds timeout
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);

Categories