I used below code in PHP. I searched few sites and worked this below curl function. In Postman Add Certificate (Settings->SSL Verification Settings) it is working by passing CRT,PEM,PFX and Passphrase. But when I'm going to use this in PHP code it is not working.
function curlapi() {
$url = "https://exampleurl.com/service";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
);
$data = '{"login":"test_login","password":"test_password"}';
$pemFile = tmpfile();
fwrite($pemFile, "/folder/path/test_file.pem");//the path for the pem file
$tempPemPath = stream_get_meta_data($pemFile);
$tempPemPath_uri = $tempPemPath['uri'];
curl_setopt($curl, CURLOPT_SSLCERT, $tempPemPath_uri);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
echo "<pre>";
print_r(curl_getinfo($curl)) . '<br/>';
echo curl_errno($curl) . '<br/>';
echo curl_error($curl) . '<br/>';
curl_close($curl);
print_r($resp);
exit;
}
curlapi();
Finally, for my concern there is no usage of CRT, PFX & Passphrase in CURL request. It works only with PEM file path.
<?php $data = array(
"PageNumber" => 1,
"PageSize" => 100,
"SearchCriteria" => array(
"keyword" => "Getvalue"
));
$pemfile = getcwd()."/assets/path/file.pem"; $curl = curl_init($url);
$method = "POST";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CAINFO, $pemfile);
curl_setopt($curl, CURLOPT_SSLCERT, $pemfile);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_POST, true);
$headers = array(
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$resp = curl_exec($curl);
/*echo "<pre>";
print_r(curl_getinfo($curl)) . '<br/>';
echo curl_errno($curl) . '<br/>';
echo curl_error($curl) . '<br/>';*/
curl_close($curl);
?>
I know I am replying this very late, but CURL request does support PFX File format with Key, Below is the code for the same
Curl::to($url)
->withHeaders($headers)
->withContentType('application/json')
->withOption('HTTP_VERSION', 'CURL_HTTP_VERSION_1_1')
->withOption('SSLCERTTYPE', 'P12')
->withOption('SSLCERT', <Path of the Certificate>)
->withOption('SSLKEYPASSWD', <passphrase used to create pfx>)
->withOption('HTTP_VERSION', 'CURL_HTTP_VERSION_1_1')
->withData(json_encode($body))
->withTimeout(30)
->post();
The main key of sending PFX file with curl request is telling CURL you are passing P12 format file for that purpose we are using :
'SSLCERTTYPE', 'P12'
Related
It seems as if $data isn't correct. I can list products and I get the whole product JSON as a response. But the product in WooCommerce doesn't change the price. When I do the same thing via curl command line, the update is working.
Referring to that: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
What am I doing wrong?
<?php
$url = "https://wooexample.com/wp-json/wc/v3/products/455";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Basic " . base64_encode ( 'ck_33fbff7b90dcddba9bd4cbedaeda6b2fa3:cs_4156da18fc357b288e42a7e7b75fa6682b' ),
);
// print_r($headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = [
'regular_price' => '24',
'price' => '24'
];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
print_r(json_decode($resp));
Ty this one, I have checked, it's working perfectly for me.
<?php
$url = "https://localhost/wordpress/wp-json/wc/v3/products/455";
$consumer_key = 'your consumer key put here..';
$consumer_secret = 'your consumer secre put here..';
$headers = array(
'Authorization' => 'Basic ' . base64_encode($consumer_key.':'.$consumer_secret )
);
$data = array(
'regular_price' => '24',
'price' => '24'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$resp = curl_exec($curl);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
print_r(json_decode($resp));
The problem lies on the data format you are sending.
the docs is using a library that's supposedly transform the data already in a supported format, while your curl request is posting raw array data via curl
you can try transforming your data into a form data with http_build_query
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data) );
or convert them into JSON format
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data) );
EDIT
You also appear to have wrong curl request, you're passing the keys on as base64_encoded authorization in headers, but based on the docs you're supposed to do
curl -X PUT https://example.com/wp-json/wc/v3/products/794 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"regular_price": "24.54"
}'
Your php curl request should have be something like
$headers = [
"Content-Type: application/json",
]
.
.
curl_setopt($curl, CURLOPT_USERPWD, "your_key_here" . ":" . "your_secret_here");
I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?
$file = "blaynmailhahaha.csv";
$authorization = "Authorization: Bearer [".$response."]";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.toku-maga.com/rest/1.0/contact/import/create");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // use HTTP/1.0 instead of 1.1
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // uncomment this line if you get no gateway
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: text/plain']);
$cfile = new CurlFile($file, 'text/csv','import');
//curl file itself return the realpath with prefix of #
$data = array('data-binary' => $cfile);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
curl_close($curl);
var_dump($cfile);
//echo "<br>Data File : " . $cfile ;
echo "<br>This is Access_token : " . $authorization ;
echo "<br>This is 2nd Output : " . $curl_response ;
I don't get any response from $curl_response and could not upload it to blaynmail. Please help me to solve this problem.
How i use it on php with curl?
curl --get --include 'https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCNPJ?cnpj={cnpj}' \
-H 'X-Mashape-Key: MYKEY'
I know this is wrong but i try this (I replace MY KEY and MY CPF for real values):
<?php
$cpf = "MY CPF";
$url = "https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCPF?cpf=".$cpf;
$data = array('X-Mashape-Key' => 'MY KEY'); //My Key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$name = trim(curl_exec($curl));
curl_close($curl);
echo $name;
?>
If you look at the man page for curl you'll see that the -H option adds a header, nothing to do with POST data. To do the same in PHP, set the CURLOPT_HTTPHEADER option:
<?php
$cpf = "MY CPF";
$url = "https://consulta-situacao-cpf-cnpj.p.mashape.com/consultaSituacaoCPF?cpf=".$cpf;
$headers = array("X-Mashape-Key: MY KEY"); //My Key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$name = trim(curl_exec($curl));
curl_close($curl);
echo $name;
?>
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....