Define host and different request addr on curl - php

I want to automatize the installation of some software on a new host for domains whose DNS servers don't direct this domain to the desired server.
Is it possible to go through the installation process using cURL? I'd need to set the REQUEST_HOST and REQUEST_ADDR to 2 different things then.
Example:
I'd like to setup a wordpress blog gowordpress.tld on machine
123.456.789.1
The DNS record for gowordpress.tld is set to 987.654.321.1
The webserver on Host 123.456.789.1 is set to servername
.gowordpress.com (nginx)
I'd like to go through the steps using a PHP script on Host
123.456.789.1
I'd like to use cURL.
Any ideas?

You have to provide IP address in URI form and specify host as one of cURL option, with CURLOPT_HTTPHEADER:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://123.123.123.123/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: gowordpress.tld') );
$res = curl_exec($ch);
curl_close($ch);

Or directly from command line you can do:
curl -H 'Host: gowordpress.tld' http://123.456.789.1/
To check only the response status and headers use the -I switch:
curl -I -H 'Host: gowordpress.tld' http://123.456.789.1/
Curl utility is available in all debian based systems: apt-get install curl

Related

CURL can not run with Domain name on local Server

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?
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.
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.

cURL Error : Cannot connect: SSL is disabled. Error number 35

I am working on the paypal ipn script in one of my applications hosted on a Digital Ocean's box with Centos 7.
When i try to connect to the paypal sandbox api i get the error "Cannot connect: SSL is disabled."
I have tried several things like adding the path of the curl.cainfo in my php.ini file like so
curl.cainfo = /etc/pki/tls/certs/ca-bundle.trust.crt
this is what my cURL script looks like
// STEP 2: Post IPN data back to paypal to validate
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); // change to [...]sandbox.paypal[...] when using sandbox to test
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'ecdhe_rsa_aes_128_gcm_sha_256');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
I have not got much experience with Linux server setup so I am learning as i go along.
Any help or guide is much appreciated
UPDATE : when i run the this command in the command line
curl --version https://sandbox.paypal.com/cgi-bin/webscr
i get this error
curl: (1) Protocol "https" not supported or disabled in libcurl
Also the command curl --version
displays curl 7.42.1 (x86_64-unknown-linux-gnu) libcurl/7.46.0
So i am guessing the new question would be how to enable https in libcurl?
You're setting the wrong SSL version:
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
The Paypal sandbox only supports TLS 1.2 (which is CURLOPT_SSLVERSION == 6). The correct SSL version will be used automatically if you use PHP 5.5.19+ and OpenSSL 1.0.1+, or you can force it yourself with the following (still requires OpenSSL 1.0.1+):
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
I would only expect to see Protocol "https" not supported or disabled in libcurl if this were running a libcurl not installed from rpm - or if whoever configured the machine deliberately broke it for a valid reason, or was very incompetent; the Centos 7 rpm for libcurl has openSSL as a requirement.
Understanding what happened here and remediation will require root access to the target host to install software. You didn't say what access you have on the target system, and you are presumably paying Digital Ocean for support. Unless you broke it yourself, you should be asking Digital Ocean to fix it.
I was able to solve this problem with the help of one my friends with a lot of Linux experience. Apparently this was an issue with the version of cURL installed on my Digital Ocean box.
So the solution was to remove the version of cURL i had installed which was not installed correctly because i had built the binaries my self.
I installed the problematic cURL using these commands
wget -O curl.tar.gz http://curl.haxx.se/download/curl-7.42.0.tar.gz
tar -xvzf curl.tar.gz
cd curl-7.42.0
./configure
make
make install
I removed the problematic cURL that had https issues like this
which curl
(To tell me where the executable was located and it was located in usr/local/bin/curl)
Then i did
sudo rm -f /usr/local/bin/curl
(To remove the cURL giving issues)
Then i used the command
sudo yum install curl php5-curl
This installed cURL correctly for me. The i ran
curl --version
To confirm that https is now supported in the cURL version and it was.
I logged out of my droplet and logged back in. Then tried the
curl --version "https://sandbox.paypal.com/cgi-bin/webscr"
command and everything worked. I was able to connect to Paypal.
Thanks to everyone for their help and comments.
I think you need to enable fopen socket.You can ask hosting provider

Can't CURL HTTPS URL on WAMP but works on remote server using BASH

Here's the BASH command I'm using on my remote server -
curl -i -H "Content-Type: application/json" -H "Authorization: USERNAME:PASSWORD" "https://api7.publicaster.com/Rest/Ping.svc/?format=json"
Here's the code on my PHP script which I run on a WAMP local test environment to request the same information.
header("Content-Type: application/json");
$encrypted_account_id = 'USERNAME';
$api_password = 'PASSWORD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api7.publicaster.com/Rest/Ping.svc/?format=json");
$headers = array();
$headers[] = 'Content-type: application/json';
$headers[] = 'Authorization: $encrypted_account_id:$api_password';
curl_setopt($ch, CURLOPT_HEADER, $headers);
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
If on my PHP script I request the HTTP site then it returns -
HTTP/1.1 401 Unauthorized
Cache-Control: private
If on my PHP script I request the HTTPS site then it just returns blank. I do have a self-signed SSL certificate and SSL is enabled, I can access HTTPS portions of my WAMP server but I am given a warning.
I can't figure out if this was an issue with my code or my WAMP server. Any ideas?
If I add the following to my code and try to CURL the HTTPS address I at least receive a response but it's the Unauthorized CC: private error -
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
I think I found the answer, the issue is your curl request could not verify the ssl peer. Windows does not automatically have a CA certificate to do the validation against.
So here is what I did to get my https requests via curl fixed for wamp.
download the cacert.pem file here http://curl.haxx.se/docs/caextract.html and place it in your PHP folder, mine was located at:
C:\wamp\bin\php\php5.5.12
Now open the php.ini file and find the line starting with
; curl.cainfo =
un-comment that line "remove the ; in front" and add the absolute path of the cacert.pem file. Mine looked something like this:
curl.cainfo = C:\wamp\bin\php\php5.5.12\cacert.pem
Now restart wamp and voila! I can happily open https protocols via curl on wamp.

Convert PHP code to curl command where only TLSv1 is allowed

How can I convert this php code to curl command? I want to use this code on linux machine by executing single curl command.
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
I tired with this one, but unsuccessful:
curl -X POST -H "Content-type: text/xml" -o output.txt -d "param1=param1&username=username&password=password" https://site.url.com -d #data.xml
Maybe the problem is in the HTTPS because only TLSv1 is allowed on the site.
In php you would use:
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
Documentation speaks of more TLS versions:
http://www.php.net/manual/en/function.curl-setopt.php
CURL_SSLVERSION_TLSv1_0
CURL_SSLVERSION_TLSv1_1
CURL_SSLVERSION_TLSv1_2
The TLS versions only work with CURL version 7.34 or newer.
If you want to force curl to use TLSv1, you can use the --tlsv1 option, from the docs:
-1, --tlsv1
(SSL) Forces curl to use TLS version 1.x when negotiating with a remote TLS server. You can use options --tlsv1.0, --tlsv1.1, and --tlsv1.2 to control the TLS version more precisely (if the SSL backend in use supports such a level of control).
-2, --sslv2
(SSL) Forces curl to use SSL version 2 when negotiating with a remote SSL server. Sometimes curl is built without SSLv2 support. SSLv2 is widely considered insecure.
-3, --sslv3
(SSL) Forces curl to use SSL version 3 when negotiating with a remote SSL server. Sometimes curl is built without SSLv3 support.
Problem is not related to TLS/SSL. Client will negotiate TLS automatically.
It seems like you need to make a POST some xml data and specify your credentials as GET parameters.
It can be done by putting your GET parameters to the request URL
Im not sure on syntax, but try this:
curl -X POST -H "Content-type: text/xml" -o output.txt https://site.url.com?param1=param1&username=username&password=password -d #data.xml
Also, (small offtopic for message above, but i cannt comment it) please not force SSL2, SSL3 or TLS1.0 since they have vulnerabilities. Most servers will negotiate best version of TLS automatically.

cURL script works when run from one server but not another

I recently moved a cURL script from one server (Ubuntu) to another (Mac OS X). The script worked when run from the Ubuntu server but it doesn't work on the OS X server. I get error 35 and this:
error:14077417:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert illegal parameter
Any ideas?
Edit: Here's what's probably a really good clue. This doesn't work:
curl -v https://foo.com/OA_HTML/RF.jsp?function_id=123
But this does:
curl -v -sslv3 https://foo.com/OA_HTML/RF.jsp?function_id=123
Now I just need to know how to do the equivalent of the second command from within PHP.
Could be some php.ini setting or apache follow or openbasedir setting !
I know I'm late but the equivalent of
curl -v -sslv3 https://foo.com/OA_HTML/RF.jsp?function_id=123
is
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION, 3); //set to force SSL3
curl_setopt($ch, CURLOPT_URL, "https://foo.com/OA_HTML/RF.jsp?function_id=123");
$result = curl_exec ($ch); //execute the request
Most of the time CURL over SSL uses OpenSSL, more than likely either CURL or OpenSSL is not installed or enabled on the new server. Save a file on the server with:
<?php phpinfo(); ?>
and check if these are enabled
ADD
Either that or your SSL Certificates where not created on the machine you are trying to use them on. Each SSL certificate has specific information about each machine (if received from an online vendor)

Categories