I was using a curl request with third party pem certificate in IIS windows based server which is:
$userAgent = 'Third party name';
$headers = array(
"SOAPAction:",
"Content-Type: text/xml; charset = utf-8",
"Connection: close"
);
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_HEADER, false);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_URL, $this->baseURL());
curl_setopt($cURL, CURLOPT_USERAGENT, $userAgent);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $soap);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_ENCODING, "UTF-8");
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_SSLCERT, $certificateURL);
curl_setopt($cURL, CURLOPT_SSLCERTPASSWD, $this->certPass);
$response = curl_exec($cURL);
curl_close($cURL);
return $response;
the $certificateURL in windows was
$certificateURL = "C:/inetpub/wwwroot/api/kx2.pem"
and it was working fine.
Now this is shifted to a Linux server and I am using plesk for this, the cert pem file is in the same directory with code but the here is I assigned
$certificateURL to "./kx2.pem" and to "/var/www/vhosts/domain.com/httpdocs/api/kx2.pem"
it doesn't work, it gives error msg "could not load PEM client certificate, OpenSSL error error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too small, (no key found, wrong pass phrase, or wrong file format?)" what should I do I have tried many things changed the whole curl request even tried __DIR__ based url?
Related
My goal is to connect a private web service on my job.
Im trying to connect but when I set Content-Type to application/json it fails and return null, someone can help?
This is my code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=UTF-8')
);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$fp = fopen(WAREHOUSE . '/errorlogc.txt', 'w');
curl_setopt($curl, CURLOPT_STDERR, $fp);
$json_response = curl_exec($curl);
when I execute this code it fails and return null and no more info, even the log I set for it show's me nothing.
fixed! the problem was that before trying to send data to the server I have to set a cookie for the session, so I get the cookie from the header response on the connection and then send the data.
thanks for the help
I am using php with curl to request some API the third-party supplies,as the API doc says,I should request the api with ssl certificate,I do.but the problem is when I run the script on my local environment(mac MAMP),the script works well,but when I deploy the script on my server(centos 6.5),the API can not response correctly,it says "error certificate",how this happen?
the code:
public function http_post_xml($data, $url){
$sslcert_path = self::DATA_PATH.'cert/apiclient_cert.pem';
$sslkey_path = self::DATA_PATH.'cert/apiclient_key.pem';
$rootca_path = self::DATA_PATH.'cert/rootca.pem';
$xml = '<xml>';
foreach($data as $k=>$v){
$xml .= "<{$k}>$v</$k>";
}
$xml .= '</xml>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($curl, CURLOPT_SSLCERT, $sslcert_path);
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, $sslkey_path);
curl_setopt($curl, CURLOPT_CAINFO, $rootca_path);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
$response = curl_exec($curl);
$rep_xml = simplexml_load_string($response, null);
$return_code = (String)$rep_xml->return_code;
return array(
'code'=>(String)$rep_xml->return_code,
'msg'=>(String)$rep_xml->return_msg
);
}
I am sure the certificate path is correct.
sorry for my bad enlish,but please help me....
I have installed curl. and configuration on my system is
cURL support enabled
cURL Information 7.35.0
Age 3
I have write code in my project is :
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_SSLVERSION,6);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $submit_url);
$curl_execute = curl_exec($curl);
curl_close($curl);
Its not giving me value on my local machine while working fine on server. What is problem?
My PHP version is
5.5.9-1ubuntu4.5
I am using ubuntu 13.10
what is problem with my local machine? what i am missing. please suggest what to do?
Edit :
If i missed any information to write, please inform me.. i am using curl very first time.
At first define the URL and post values correctly, I have added examples, please try this:
// define url examples
$submit_url = 'http://localhost/phptest/service.php';
// post fields examples
$postfields = 'name=foo&pass=bar&format=json';
// Start the process:
$curl = curl_init();
// Tell cURL to fail if an error occurs:
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// Allow for redirects:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// Assign the returned data to a variable:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Set the timeout:
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// Use POST:
curl_setopt($curl, CURLOPT_POST, 1);
// Set the POST data:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_URL, $submit_url);
// Execute the transaction:
$result = curl_exec($curl);
// Close the connection:
curl_close($curl);
// Print the results:
print_r($result);
How do you implement https://developers.google.com/youtube/2.0/developers_guide_protocol_deprecated using PHP?
What I tried:
<?php
$headers = array("PUT /feeds/api/users/default HTTP/1.1",
"Host: gdata.youtube.com",
"Content-Type: application/atom+xml",
"Authorization: Bearer ACCESS_TOKEN",
"X-GData-Key: key=DEVKEY",
"Content-length: ".strlen($data),
$curl = curl_init("https://gdata.youtube.com/feeds/api/users/default?v=2.1");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
$returnxxx = curl_exec($curl);
curl_close($curl);
echo $returnxxx;
?>
I am missing ACCESS_TOKEN though.
Associating an unlinked Google Account with a YouTube channel is no longer possible via the APIāthat's why it's in the deprecated section of the documentation.
The current way of prompting a user to link a Google Account to a channel is described at https://developers.google.com/youtube/create-channel That guide is mobile-focused, but the same process can be kicked off by redirected a user to https://www.youtube.com/create_channel on the desktop.
I need some assistance rewriting this PHP curl code that uses *.pem (CA cert), Client cert and private key in one file:
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSLCERT, $this->keystore);
curl_setopt($curl, CURLOPT_CAINFO, $this->keystore);
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $this->keystorepassword);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
So it could use CA certificate, Client Certificate and Private Key in separate files.
As in this command-line example:
curl -d "var1=value1&var2=value2&..." -G -v --key key.pem --cacert ca.pem --cert client.pem:xxxxxx https://www.somesite.com/page
Here is a PHP script with a literal translation of your command line call:
<?php
$data = "var1=value1&var2=value2&...";
$url = "https://www.somesite.com/page";
$keyFile = "key.pem";
$caFile = "ca.pem";
$certFile = "client.pem";
$certPass = "xxxxxx";
// Initialise cURL
$ch = curl_init($actualUrl);
// The -d option is equivalent to CURLOPT_POSTFIELDS. But...
// PHP's libcurl interface does not implement the -G flag - instead you would
// append $data to $url like this:
$actualUrl = $url.'?'.$data;
curl_setopt($ch, CURLOPT_URL, $actualUrl);
// The -v flag only makes sense at the command line, but it can be enabled
// with CURLOPT_VERBOSE - in this case the information will be written to
// STDERR, or the file specified by CURLOPT_STDERR. I will ignore this for
// now, but if you would like a demonstration let me know.
// The --key option - If your key file has a password, you will need to set
// this with CURLOPT_SSLKEYPASSWD
curl_setopt($ch, CURLOPT_SSLKEY, $keyFile);
// The --cacert option
curl_setopt($ch, CURLOPT_CAINFO, $caFile);
// The --cert option
curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
/*
Now we should get an identical request to the one created by your command
line string, let's have a look at some of the other options you set...
*/
// CURLOPT_HEADER is disabled by default, there's no need for this unless you
// enabled it earlier
//curl_setopt($ch, CURLOPT_HEADER, 0);
// Your command line string forces a GET request with the -G option, are you
// trying to POST or GET?
//curl_setopt($ch, CURLOPT_POST, true);
// We don't need body data with a GET request
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// Since we've gone to all the trouble of supplying CS information, we might
// as well validate it!
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
The following code can be used to send a post request with key and cert.
This code is equvalent to the following curl
curl --cert yourkey.pem --key yourcert.key -d
'grant_type=client_credentials&client_id=1&client_secret=2'
https://accounts.youraccount.com/auth/oauth/v2/token
$CURLOPT_URL= "https://accounts.youraccount.com/auth/oauth/v2/token";
$CURLOPT_POSTFIELDS= "grant_type=client_credential&client_id=1&client_secret=2";
$clientCert = dirname(__FILE__) . '/certificates/yourkey.key';
$clientKey = dirname(__FILE__) . "/certificates/yourcert.pem";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $CURLOPT_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $CURLOPT_POSTFIELDS);
curl_setopt($ch, CURLOPT_SSLKEY, $clientCert);
curl_setopt($ch, CURLOPT_SSLCERT, $clientKey);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
curl_setopt($ch, CURLOPT_SSLKEY, $clientKey);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
print_r($result);
curl_close($ch);
exit;