Curl : PHP KO, cmd OK - php

I need some help, here the code :
<?php
$curl_options = array (CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_MAXREDIRS => 5 );
$handle = curl_init("http://my.awesome.subdomain.com");
curl_setopt_array($handle, $curl_options);
$content = curl_exec($handle);
print_r(curl_error($handle));
print_r($content);
die();
my.awesome.subdomain.com is a vhost. On the same machine i have another.awesome.subdomain.com which is on the same machine.
Via SSH, if i do $> curl my.awesome.subdomain.com I get the answer i expect. But when i use this code, i get "Connection timed out after 30180 milliseconds"
Thanks for help and sorry for my writing errors.

Related

Problems with PHP for a website for a FiveM server

I have a PHP problem with my web page. I state that I am developing the site for a FiveM server.
In this page I have to enter the number of players and the number of maximum players that the FiveM server can host.
This is what I wrote:
<?php
$file = file_get_contents('http://fivem.lrfreeroamitalia.it:30120/dynamic.json');
$decode = json_decode($file, true);
$clients = isset($decode['clients']);
$svmaxclients = isset($decode['sv_maxclients']);
echo $decode['clients'] . '/' . $decode['sv_maxclients'];
?>
The problem with this code is that it gives me this error PHP:
Warning: file_get_contents(http://fivem.lrfreeroamitalia.it:30120/dynamic.json): failed to open stream: Connection timed out in /web/htdocs/www.lrfreeroamitalia.it/home/index.php on line 49
Port 30120 of the remote server is open.
P.S. I use Aruba.it as a provider
You use cURL to grab contents and display them.
More Documentation on cURL: https://www.php.net/manual/en/curl.examples.php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://fivem.lrfreeroamitalia.it:30120/dynamic.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response);
echo $obj->clients . '/' . $obj->sv_maxclients;

PHP Curl Not connecting to a url with IP Address and PORT

Have made and deployed an app on an AWS EC2 instance, and accessing the app via ip_address:port, where ip_address is the elastic ip associated with the instance.
I am able to make API calls to this app using postman, and via cmd.
However, when I try to make POST request using PHP Curl on Wordpress (hosted on Bluehost), I keep getting the
Failed to connect to ip_address port XXXX after 0 ms: Connection refused.
Any ideas of what I might be missing?
'''php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http:ip_address:8000/payments/accounts/v1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_CUSTOMREQUEST => 'POST',));
print_r(curl_error($curl));
curl_exec($curl);
curl_close($curl);
'''
Getting this response
'''code
Failed to connect to id_address port 8000 after 0 ms: Connection refused
'''
Using the redacted ip and a slightly modified version of your code as follows:
$url='http://X.XXX.XX.XX:8000/payments/accounts/v1';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST=>true
)
);
$res=curl_exec( $curl );
curl_close( $curl );
printf('<pre>%s</pre>',print_r( $res, true ));
Yields:
{"detail":"Authentication credentials were not provided."}
Perhaps if you were to actually include and submit some post data in the CURLOPT_POSTFIELDS option you might find a different response.

PHP CuRL - Empty response due to SSL

I'm going to ask a question that's already been asked a lot, but I can't find a solution that works on my server.
I make a call with PHP on an API where I am correctly identified. I get the famous error 52 (Empty reply from server).
I tried all the parameters indicated on Stack but nothing works. I'm on a Ngnix server with an SSL Lets encrypt.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://xxx/route/rest?=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 100,
CURLOPT_TIMEOUT => 300,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "app_key=" . env('APP_KEY')
));
$response = curl_exec($curl);
If you could help me, I would be very grateful.
Have a nice day
Eliott

How to get the http method used in curl_php()

How to get the HTTP method(like GET,POST,DELETE) used in the curl_php connections? It is not avaiable in curl_getinfo.
The code for the connection is
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
);
$response = curl_init($url);
curl_setopt_array($response, $options);
$content = curl_exec($response);
$options_debug = curl_getinfo($response);
curl_close($response);
By default cURL methods are always GET unless you specify it differently. Also, if you invoke cURL with -d or --data the request will be POST.
POST request override default GET when using -d parameter.
More info here: cURL info
BR

CURL not working with link in PHP, but in command line it does

I've got a problem with my PHP script using CURL. I want to download file from another server (Probably with hotlink protection). The URL is like this: example.com/script.extension?id=12345&type=xxx&another=qwerty
When i hit that link from my website, It sends 'Referer:' in headers, so the download is not starting. (without the header it works)
I have to modify the file's name, so i've used CURL to download that to my server. When I'm doing that with that script, the file is created, but it is empty:
function my_function($the_url) {
$fp = fopen ('tmp_file.extension', 'w+');
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_VERBOSE => 1,
CURLOPT_URL => $the_url,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POST => false,
CURLOPT_USERAGENT => $user_agent,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => false,
CURLOPT_REFERER => "http://example.com/",
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$my_exec = curl_exec($ch);
fwrite($fp, $my_exec);
curl_close($ch);
fclose($fp);
}
But it works well with file which is hosted on my site (eg. mysite.com/file.zip)
Also, when I run command through the shell:
$ curl -L -o file.tmp --referer http://example.com/ full url here
It downloaded the file properly.
What's the reason I cannot do it with my PHP function?

Categories