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.
Related
So here's my problem.
I'm using curl to access my CouchDB by HTTP. I recently updated my WAMP to the WAMP 3 64bit wich comes with PHP 5.6.16 and Apache 2.4.17. Therefore, since this upgrade, I discovered that I couldn't do PUT request anymore.
Env
PHP 5.6.16
Apache 2.4.17
Windows 10 64 bit
Wamp 3 64 bit
Curl --version
curl 7.49.1 (x86_64-pc-win32) libcurl/7.49.1 OpenSSL/1.0.2h nghttp2/1.11.1
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM SSL HTTP2
Code executed
So when I execute this :
<?php
$table="testname";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5984/' . $table);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'validUser:validPass');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
I get a quick response from the server.
Then, I try to create a database :
<?php
$table = "testname";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5984/' . $table);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'validUser:validPass');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Problem
So, when I execute this code, the request will hang on curl_exec.
What's weird is that, after the timeout, the request will be received by CouchDB but no response will be given. It seems that my "Put" request are stacked in a buffer and they are waiting to be executed.
Verbose curl output
* Hostname in DNS cache was stale, zapped
* Trying ::1...
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5984 (#0)
* Server auth using Basic with user 'validUser'
> PUT /customers HTTP/1.1
Host: localhost:5984
Authorization: Basic dGVzdEFkbWluOnRlc3RQYXNzd29yZA==
Content-type: application/json
Accept: */*
* Operation timed out after 10000 milliseconds with 0 bytes received
* Closing connection 0
Hints
-I try to install a SSL certificate but It didn't seem to work. Having this certificate still installed can cause problems?
-I can do PUT request with a REST client on my Atom editor without problems.
-I seems like there is a problem in my network route internally. I'm saying this because It affected the PHP-Curl aswell as the Curl CLI. Also, I'm able to do GET request but the PUT request are like "hanging" for no reason and are "Accepted" by my CouchDB when the timeout occurs. It's like if I was sending long poll request.
What have been tested
Execute the same command on the command line -> Same result
Try a REST Client on my Atom editor with success
A friend of mine try to access to my database remotly with success (So CouchDB doesn't seem the problem)
Even if I tested with my Firewall disabled, uninstalling my antivirus ( Bitdefender Total Security 2016) fixed my issue.
I'm having trouble with php curl on my localhost for days. It seems that I'm not reaching the CURLOPT_URL parameter. curl_error and curl_errno says, "Recv failure: Connection was reset". The url is supposed to return a processed value of the passed post data. I'm using windows 7 machine and running XAMPP 3.2.2
Appreciate your help guys. Thanks
Below is my code:
private function getCurl ($credentials = array(), $app_code = "")
{
$ch = curl_init();
$data = array('Code' => $code,'type' => 'credentials');
curl_setopt($ch, CURLOPT_URL,'http://localhost/web/service.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' .
base64_encode($credentials['user'] . ":" . $credentials['pass'])));
$curl_response = curl_exec($ch);
//return array(curl_error($ch),curl_errno($ch));
curl_close($ch);
return $curl_response;
}
Apache error logs: [Thu Apr 14 07:46:54.089533 2016] [ssl:warn] [pid 3372:tid 252] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
There could be a number of reasons why this is happening. I think you should use the XAMP Control Panel to view the specific errors.
Quite possible that another application such as Skype is using port 80. In this case you may want to check using netstat -ano and find who is using port 80. Close the Program which uses port 80 and try to start apache again.
It may be due to your rights. You will need to be administrator.
Disable Anti-virus (Try first to disable skype and running again, if it didn't work do this step)
Right click on xampp control panel and run as administrator
server name used in Apache (httpd.conf) must be the same as the server name in apache (httpd-ssl.conf) e.g. in Apache (httpd.conf)ServerName localhost:8080 then in apache (httpd-ssl.conf) should be like this ServerName www.example.com:8080
You can also try to change the Configuration of Apache.
a) Select Apache (httpd.conf)
b) searched for this line: Listen 80
c) changed for this: Listen 8081
d) saved file
Further...
a) Select Apache (httpd-ssl.conf)
b) searched for this line: Listen 443
c) changed for this: Listen 444
d) saved file
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.
I am currently trying to query an online API. Some example code:
$ch = curl_init("https://gdata.youtube.com/feeds/api/users/UC_x5XG1OV2P6uZZ5FSM9Ttw/playlists");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 50,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
]);
$data = curl_exec($ch);
curl_close($ch);
And I get: curl(7) couldn't connect to host
If you do the same thing on the CLI everything works:
$ curl -vvv "https://gdata.youtube.com/feeds/api/users/UC_x5XG1OV2P6uZZ5FSM9Ttw/playlists"
* About to connect() to gdata.youtube.com port 443 (#0)
* Trying 173.194.113.1... connected
...
I first suspected IPv6 to be the problem, but I deactivated it. It doesn't help:
$ sudo sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Since you already have CONNECTTIMEOUT it might not be a curl timeout.
The timeout could be for PHP execution.
You can try a set_time_limit(0) to get over this and see if it helps.
Also try curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); to get over proxy configurations
I was having a slightly different issue with a curl timeout via PHP but it was similar to your issue as it worked fine via the command line. I found that my PHP code worked once I set the User-Agent header as such:
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)');
My only thought is that some sites are only issuing a response for what they consider valid User-Agents.
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