Reputation: 145
i'm trying to use curl to run a local php file inside my centOs7 shared server, but i can not use curl method for my local files. here are my example files :
--bg/
------back.php
------curl.php
here is curl.php codes:
<?php
function run_curl($data){
//url-ify the data for the POST
$fields_string = '';
//foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = http_build_query($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
//curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
curl_setopt($ch,CURLOPT_URL,'https://taskdan.com/bg/back.php');
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,50); # timeout after 10 seconds, you can increase it
//curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); # Some server may refuse your request if you dont pass user agent
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//print_r($result);
//print_r((curl_getinfo($ch)));
//error_log($result);
//close connection
curl_close($ch);
return ($result);
}
echo " Output : </br>";
echo run_curl( [
"ab" => "test"
]);
?>
and the below my back.php codes:
<?php
header("Content-Type:application/json");
echo json_encode([
"a" => "test"
])
?>
When I run this code, it looks like it converts my domain name to the server's public IP address. Curl is working fine for outside of server scripts.
when i run curl with using SSH, i get this:
[root@??? ~]# curl https://taskdan.com -k
<html>Apache is functioning normally</html>
[root@??? ~]# curl http://171.22.27.118 -k
<html>Apache is functioning normally</html>
i want to run some scripts in background, Are there any way to run a local php file with POST parameters by CURL?
Upvotes: 0
Views: 2892
Reputation: 935
Are you sure that your Apache virtual host is configured for listening on all interfaces? When you do a DNS lookup to a domain that is configured as 127.0.0.1 in your /etc/hosts the request is then pointed to the loopback interface. Open your virtualhost configuration and search for:
<VirtualHost ...>
If it is configured as
<VirtualHost 171.22.27.118:80>
Change it to
<VirtualHost *:80>
..and try again
If you cannot edit the VirtualHost definition for any reason you can instruct cURL to make the request on a specific address/port and set the correct Host header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Host: taskdan.com'
)
);
curl_setopt($ch,CURLOPT_URL,'https://171.22.27.118/bg/back.php');
This could help mitigate the issue but is not guaranteed to work as the problem is the webserver configuration not the source code per se.
Upvotes: 1
Reputation: 42755
Your /etc/hosts
file should look like this:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Change it to look like this:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 taskdan.com
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 taskdan.com
Or, just change your code.
curl_setopt($ch,CURLOPT_URL,'http://127.0.0.1/bg/back.php');
And configure Apache accordingly.
Upvotes: 1