file_get_contents on other port - php

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);

Related

PHP cURL connection refused while using proxy

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?

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?

PHP whois via proxy

I have this PHP script to fetch whois information of domain. It works, but when I try to connect whois server via proxy, then it doesnt work. What I do wrong? Thank you for help.
$server = "whois.nic.cz";
$domain = "ucedan.cz";
function QueryWhoisServer($server, $domain){
$proxy = "85.111.25.189:8080";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $domain . "\r\n");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

cURL request works on localhost but Connect() times out on server

OK, so I apologise if this has been dealt with before, after searching for 30 minutes I couldn't find anything that exactly matched my issue.
I have a bit of a strange situation where a client is using an FTPS connection which I need to use cURL to download a file from. While running this EXACT script on my localhost the files download with no issue however once I place this on the server (our own hosting) the cURL returns a timeout error.
$remote = [
filename => URL
];
ini_set("display_errors", 1);
foreach ($remote as $key => $file) {
$targetFile = file formatted;
$sourceFile = $file;
$ftpuser = username;
$ftppassword = password;
echo $targetFile;
// function settings
$timeout = 10;
$fileOpen = 'w';
$curl = curl_init();
$file = fopen($targetFile, $fileOpen);
curl_setopt($curl, CURLOPT_URL, $sourceFile);
curl_setopt($curl, CURLOPT_USERPWD, $ftpuser . ':' . $ftppassword);
// curl settings
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_FTP_SSL, CURLFTPSSL_ALL);
//curl_setopt($curl, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_FILE, $file);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
var_dump ($info);
echo curl_error($curl);
curl_close($curl);
fclose($file);
Most likely the server is being blocked by a proxy server, preventing the connection from being established.

curl returns error 111 socket

i got this simple curl code below:
$target = "/plugin/api_register.php?name=$activation_name&key=$activation_key&url=$siteurl";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com'.$target);
curl_setopt($ch, CURLOPT_REFERER, $siteurl);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$output = curl_exec($ch);
if (empty($output))
{
// some kind of an error happened
die(curl_error($ch));
}
curl_close($ch);
print_r($output);
all i get is socket error: 111
and i cant figure out why it wont connect.pls help
Errno 111 is for connection refused.
There could be several reasons
No http server running
TCP queue full
Firewall blocks your request

Categories