I've got this piece of code to launch queries with curl:
function curl_query($full_url, $username, $password, $payload) {
$additionalHeaders = "";
$process = curl_init($full_url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payload);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_MAXREDIRS, 4);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
$return = curl_exec($process);
if ($return === false) {
error_log("CURL error ".curl_error($process));
}
return $return;
}
The option CURLOPT_SSL_VERIFYPEER is set to false so I can read pages with self-signed certificates. However when I execute this code against an https URL I get an error:
CURL error SSL: certificate subject name 'localhost' does not match target host name '192.168.1.1',
I run this code on a CentOS 5 server with php53 package installed.
Thanks in advance
Add the option CURLOPT_SSL_VERIFYHOST
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, FALSE);
I realized, that the english version of the PHP documentation missing this important information:
(translated from the german version of the PHP manual)
CURLOPT_SSL_VERIFYHOST has to be set to true or false if CURLOPT_SSL_VERIFYPEER has been deactivated.
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($process,CURLOPT_SSL_VERIFYHOST, FALSE);
ADD THIS TO LINES WILL HELP
Related
Currently, i am working on PHP CURL to post data to call API.
I have implemented PHP cURL to POST client_id, client_secret value to my API. CURL is working in my local server, but on my production server it's throwing an error.
Error: "cURL error (7): Couldn't connect to server".
PHP Version 5.6.24
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
if($_POST['grant_type'] != "authorization_code")
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$client_secret");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYPORT, "443");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_USERAGENT, "spider");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($ch);
These are security protocols in most Linux distros. You check SELinux on the command line by typing: getenforce You check FirewallD by typing: sudo firewall-cmd --state Use man firewall-cmd to get more options.
You can use sudo firewall-cmd --list-all to find the zones allowing http/https. Just let me know what you find, and I can help you with additional commands.
$process = curl_init();
$temporary_cookie_file = 'temporaryfiles/'.strtolower($user_general_information['Username']).'_cookies.txt';
$temporary_cookie_file_handler = fopen($temporary_cookie_file, 'w');
fclose($temporary_cookie_file_handler);
$user_data = array('TCNK'=>'authenticationEntryComponent', 'submitEvent'=>'1', 'guestLoginEvent'=>'', 'enterClicked'=>'true', 'bscf'=>'', 'bscv'=>'', 'targetEntid'=>'', 'ajaxSupported'=>'yes', 'screenName'=>$user_general_information['Username'], 'kclq'=>$user_general_information['Password']);
curl_setopt($process, CURLOPT_URL, 'https://www.edline.net/post/InterstitialLogin.page');
curl_setopt($process, CURLOPT_USERAGENT, 'Chrome/12.0.742.122');
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($user_data));
curl_setopt($process, CURLOPT_TIMEOUT, 0);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_COOKIEJAR, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_COOKIEFILE, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_HEADER, false);
$results_1 = curl_exec($process);
var_dump($results_1);
die();
I am trying to use POST form data to log into a site and then store the created cookies in a temporary file. I've looked at other discussions that talk about doing this and did as them but it doesn't work. When I try, it simply refreshes the page with no changes. I've been working on this for hours...Any ideas of what I am doing wrong?
I'm trying to get some data from an api that is protected by http authentication.
I'm want the response to be a json, but I'm getting a string innstead.
What can I do to fix this?
This is my code:
//init curl
$ch = curl_init();
$process = curl_init($url."?".$queryString);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json', ''));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, '');
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
print_r($return);
Am not familiar with Php and I have a problem with running this code on windows curl_php is enabled and there no errors when I execute the code but i have a blank screen while on ubuntu I got a result , I also disabled firewall but still nothing
$username = "myusername";
$password = "mypasword";
$host = "https://myurl";
$process = curl_init($host);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLINFO_HEADER_OUT, false);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_CUSTOMREQUEST, 'GET');
echo curl_exec($process);
The solution is to disable ssl verifying (yep, it uses https =) )
Code:
<?php
$username = "myusername";
$password = "mypassword";
$url = "https://myurl";
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLINFO_HEADER_OUT, true);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false);
echo curl_exec($process);
curl_close($process);
Try to debug. Add the following code to beginning of php file right after <?php (on new line):
error_reporting(E_ALL|E_NOTICE);
ini_set('display_errors', 'On');
And refresh your page. You wil probably see an error if it exists;
If not, add the following after last curl_setopt call:
echo 'errno: ', curl_errno($process), ', error: ', curl_error($process), "\n";
You will probably see an error.
After that we can try to help you to solve the problem;
Maybe there's a redirect to another page, and your windows php installation uses open_basedir restriction. Or maybe something else =)
I had a problem. When you send a POST request with the CURL library to HTTPS get the error: SSL certificate problem, verify that the CA cert is OK. Details: error: 14090086: SSL routines: SSL3_GET_SERVER_CERTIFICATE: certificate verify failed. using current certificate.
I tried various certificates FROM http://www.startssl.com/certs/ and FROM http://curl.haxx.se/docs/caextract.html
Tell me what could be the cause of the error?
Here's the code POST request:
curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , '');
curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($process, CURLOPT_TIMEOUT, 120);
curl_setopt($process, CURLOPT_PROXY,$this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process,CURLOPT_VERBOSE,1);
if($ssl){
curl_setopt ($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($process, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($process ,CURLOPT_CAINFO, YiiBase::getPathOfAlias('webroot').'/files/GTECyberTrustGlobalRoot.crt');
}
curl_setopt ($process, CURLOPT_HTTPHEADER, array('Expect:'));
$return = curl_exec($process);
$this->error_code = curl_getinfo($process, CURLINFO_HTTP_CODE);
Here is a working example. You should take a look a your options (reduce the number of option for test) and just set the CURLOPT_SSL_VERIFYPEER to false in order to disable the CA check.
// connect via SSL, but don't check cert
$handle=curl_init('https://www.google.com');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);
echo $content; // show target page
check HERE