API Key in curl - php

So my question is the following, using xampp for localhost, using PHP 7+ and curl, i need to make an API post call. it was given to me an API Key, that i don t know how to validate. this is the code:
<?php
$curl = curl_init();
$data_array = array(
"amount" => '1',
"operative"=> "AUTHORIZATION",
"signature"=> "KKbAWfePbyt41T7inU8ulGtR",
"customer_ext_id"=> "IDInterno",
"additional"=> "Additional",
"service"=>"CE4C4D1C-2A2C-4AA7-BE59-62E497AEFE16",
"secure"=> true,
"url_post"=> "https://demo.pagaqui.pt:5135/RegisterCard.aspxOk: ",
"url_ok"=> "https://carteira.pagaqui.pt/repo/ok.htmlKO: ",
"url_ko"=> " https://carteira.pagaqui.pt/repo/nok.html ",
"template_uuid"=> "43D8016C-D28C-4F44-899A-2195CA8DC023",
"description"=> "Desc"
);
$data_array = http_build_query($data_array);
curl_setopt($curl, CURLOPT_URL, 'https://api-gateway.pagaqui.pt/v1/sandbox/payment');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_array);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$err = curl_error($curl);
if($err){
echo 'Curl Error: ' . $err;
}
else {
print_r($response);
}
<?php
My response in the localhost is
{"message":"Unauthorized","code":401,"details":""}
Thank you for the attention.

Already solved it, just needed to create a variable to the key, and after that, inside curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer ".$Key, 'Content-Type: application/json' ));
Thank you.

Related

Monnify Transaction Verification Issue,

I ma currently integrating the Monnify payment system but I'm getting errors.
Here is my code;
public function validatePayment(Request $request){
//dd($request->input());
$url = ENV('MONNIFY_BASE_URL').'/api/v2/transactions/'.urlencode($request->transactionReference);
//dd($url);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer ".base64_encode(ENV('MONNIFY_API_KEY').":".ENV("MONNIFY_SECRET_KEY")),
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
$resp = json_decode($resp, true);
dd($resp);
}
When i die dump respose, this is what I get
^ array:2 [▼
"error" => "invalid_token"
"error_description" => "Cannot convert access token to JSON"
]
Here is a link to the documentation
https://teamapt.atlassian.net/wiki/spaces/MON/pages/213909851/Get+Transaction+Status
What could be problem?
For any one who wants to try this, here are the test keys
MONNIFY_API_KEY=MK_TEST_FWD6HTML35
MONNIFY_SECRET_KEY=5Y8ZBQMSEUXMTQMHFAV3U3A34NNYH1X5
MONNIFY_BASE_URL=https://sandbox.monnify.com
Also here the test transaction reference that I want to verify
MNFY|05|20220703011128|000322

How to connect with monday.com to create lead or deal in monday.com using curl php?

I checked the API documentation but there are no examples related to curl php.
Can i get some guide on how to connect with monday.com to create lead or deal in monday.com using curl php?
I have sample code (Token is wrong in this code snippet), but I have no idea on how to pass data to create lead
<?php
$token = 'eyJhbGciOiJIUzI1NiJ9.0Y-0OesftWBt2SamhvuPV5MR-0Oq7iApMt2exFkDNdM';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
$query = '{ boards (limit:1) {id name} }';
$data = #file_get_contents($apiUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode(['query' => $query]),
]
]));
$responseContent = json_decode($data, true);
echo json_encode($responseContent);
?>
I'm not familiar with this monday.com page, but this is how you can make a cURL request in PHP:
<?php
$token = 'eyJhbGciOiJIUzI1NiJ9.0Y-0OesftWBt2SamhvuPV5MR-0Oq7iApMt2exFkDNdM';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];
// Payload
$query = '{ boards (limit:1) {id name} }';
$payload = ['query' => $query];
// Init cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// Exec cURL
$resp = curl_exec($curl);
// Close cURL
curl_close($curl);
// Get response
$response = #json_decode($resp, true);

What type of JSON is and how to parse it with php?

How to extract the "SALECODE" and "PRICE" from this curl get request in php for later MySql compare and insert if it's different, and what type of JSON is the result?
?php
$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);
//print_r($result);
$json = json_decode($result, true); print_r($json);
echo $json["SALECODE"];
?>
Sample Response:
{
"result":[
{
"DATASET":[
{
"SUPP_UM":"Kg",
"DETAILS":"70025",
"PRICE":"1.5",
"REC":1,
"CATEG_10":"-",
"NOTES":"",
"LASTSUPPLIER":"Furnizor 2",
"CATEG_9":"-",
"CATEG_8":"-",
"CATEG_7":"-",
"CATEG_6":"-",
"CATEG_5":"-",
"CATEG_4":"-",
"CATEG_3":"-",
"CATEG_2":"Paine",
"CATEG_1":"Panificatie",
"DIVISIBLE":0,
"SALECODE":"12",
"VAT_LETTER":"B",
"ISALT_PRICE":0,
"VAT_VALUE":"9",
"LISTED":1,
"LASTSUPPLY_UNIT_COST":"4",
"SUPP_UM_RATIO":"3.33",
"IDEXTAPP":"70025",
"IDSMARTCASH":1,
"DESCRIPTION":"12",
"LASTSUPPLY_DATE":"2015.01.05 00:00:00",
"SALE_UM":"Buc",
"IDSMARTCASH_LASTSUPPLIER":2,
"ALT_UM":"Kg",
"DISCOUNT":"0",
"LASTSUPPLY_QTY":"3",
"LASTSUPPLY_NIR":5,
"RECVERSION":284,
"NAME":"PAINE DE SECARA 300G",
"ITEM_CODES":[
{
"SALECODE":"12",
"ITEM":1,
"SALECODE_NAME":"PLU"
}
],
"SALECODE_NAME":"PLU",
"ALT_UM_RATIO":"0.3",
"IDEXTAPP_LASTSUPPLIER":"",
"CODWITHCRC":"12"
}
]
}
]
}
Try using $json['result'][0]['DATASET'][0]['SALECODE'] instead.
If you look at your response data closely, you'll see it's heavily nested. result is also an array, so you'll need to choose an item from it. In this case, there's only one. If there are others, you'll need to loop over $json['result'].
<?php
$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);
$json = json_decode($result, true);
echo "Price: " . $json['result'][0]['DATASET'][0]['PRICE'];
echo "<hr>";
echo "Sales code: " . $json['result'][0]['DATASET'][0]['SALECODE'];
?>

cake php http GET with bearer key?

Hi I'm using following code to send Https GET request to another api
$access_token = $this->Session->read("AUTH_BEARER");
$url = GET_DOCUMENT_BY_ID_URL.'202';
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$access_token));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);// comment this line in live servers
$response_json=curl_exec($curl);
$error= curl_errno($curl);
$error_message= curl_error($curl);
echo "error:".$error;
echo "<br>";
echo "Error message:".$error_message;
echo "<br>";
curl_close($curl);
$response_array = json_decode($response_json,true);
echo $response_json;
But I didn't receive any response from server. I make this call as soon as I receive bearer token, so I hope I made the call before token expire. what I'm doing wrong here? can someone help me to solve this? Thanks in advance.Note: I'm using https:// not http://
nothing to do with cakephp here , if You only want to send GET request , you dont need to use curl , you can use file_get_contents like the following example
$url = 'http://www.example.com/index.php?xxxx=112';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$access_token
));
$context = stream_context_create($options);
$webservice= file_get_contents($url, false, $context);
echo $webservice;

Calling Watson API from PHP code

I am trying to call Natural Language Understanding Watson API from my PHP code using curl. I have successfully tried curl from terminal. It gives some result on executing this command:
curl -u "my_username":"my_password" "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=I%20still%20have%20a%20dream.&features=sentiment,keywords"
However when I try to use curl in php using this code:
<?php
$url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=I%20still%20have%20a%20dream%2C%20a%20dream%20deeply%20rooted%20in%20the%20American%20dream%20%E2%80%93%20one%20day%20this%20nation%20will%20rise%20up%20and%20live%20up%20to%20its%20creed%2C%20%22We%20hold%20these%20truths%20to%20be%20self%20evident%3A%20that%20all%20men%20are%20created%20equal.&features=sentiment,keywords";
$post_args = array(
'version' => '2017-02-27',
'url' => 'https://davidwalsh.name/curl-post',
'features' => 'sentiment,keywords'
);
$headers = array(
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "my_username:my_password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_args));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Send Error: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
?>
I get this result:
{
"error": "unsupported media type",
"code": 415
}
What should be done?
I can't believe I was stuck here due to a blank space.
Replacing this:
$headers = array(
'Content-Type:application/json'
);
with
$headers = array(
'Content-Type: application/json'
);
worked. Note the blank space between : and application/json.

Categories