Returning false on running a CURL request from a url with a port on Cpanel

Hello team,

I have the following code in PHP and whenever I run it from my cpanel server, the result is always false. Could it be as a result of security or something else? Do not mind the URL and credentials, those are wrong I just wanted to demonstrate my problem.

		<?php
		header("access-control-allow-origin: *");
		$username = 'admin';
		$password = 'Pass*#21_!';

		$url='http://12.113.12.186:8083/api/dataValueSets.json?dataSet=%&period=%&orgUnit=%';	

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
		curl_setopt($ch, CURLOPT_USERPWD, "".$username.":".$password."");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($ch);

		curl_close($ch);

		echo $output;
		?>

The same code is working perfectly well for URLs which are in letters and don’t have a port number like:

		<?php
		header("access-control-allow-origin: *");
		$username = 'admin';
		$password = 'district';

		$url='https://play.dhis2.org/2.34.0/api/dataValueSets.json?dataSet=%&period=%&orgUnit=%';	

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
		curl_setopt($ch, CURLOPT_USERPWD, "".$username.":".$password."");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($ch);

		curl_close($ch);

		echo $output;
		?>

The result is : {“httpStatus”:“Conflict”,“httpStatusCode”:409,“status”:“ERROR”,“message”:“At least one valid organisation unit or organisation unit group must be specified”}

This is exactly the result that I want for the URL with numbers and a port.

Hello @mountain

  1. Can you copy paste below URL duly filled and fire in the browser to see it works?

http://12.113.12.186:8083/api/dataValueSets.json?dataSet=%&period=%&orgUnit=%

  1. This system is configured for http right? not https?

  2. Can you see whether your password’s special characters need any escape characters in php.

Hi @jthomas,
Thank you for your interest.

  1. When I run this code from the browser from my localhost, it is successful. What I normally do is first log in on that DHIS2 instance and everything runs well. But since am building an App, I want the PHP file pulled from my online server.

  2. The system is http configured.

  3. This line curl_setopt($ch, CURLOPT_USERPWD, “”.$username.“:”.$password.“”);, helps to post the variables with the special characters as is. The same code works on my localhost.

@mountain

Is that online server a unix variant? You can ping from that online server to DHIS2 server ?

You can also try telnet 12.113.12.186 8083 from online server to see port 8083 is reachable.

Additionally you can configure nginx to avoid the using port explicitily

Hi @jthomas,
I included “curl_error($ch)” to get the error and it was “Connection refused”. I will check with the network connection restrictions both on my cpanel and the server where the DHIS2 instance is being hosted.
Thank you.

1 Like