I'm using swifdil api to create users from a html form via curl.
The API expects a certain format (json) to receive but I have no idea how I can achieve the format the API looks for.
Example code by the API:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.swiftdil.com/v1/customers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"type\" : \"INDIVIDUAL\",\r\n \"email\" : \"Maria#Papakiriakou.com\",\r\n \"first_name\" : \"Maria\",\r\n \"last_name\" : \"Papakiriakou\"\r\n}",
I need to submit the values through an HTML form so I did the following:
function createNewUser()
{
// First we get all the information from the fields we need to pass on to swiftdill.
$type = $_POST['type'];
$email = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$fields = array(
'type' => $type,
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name
);
json_encode($fields);
$fields_string = http_build_query($fields);
$curl = curl_init();
// Set the options for the curl.
curl_setopt($curl, CURLOPT_URL, "https://sandbox.swiftdil.com/v1/customers");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURL_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer " .$newToken. "",
"Cache-Control: no-cache",
"Content-Type: application/json",
"Postman-Token: 0c513fa9-667d-4065-8531-8c4556acbc67"
));
The output of my code is as follows:
type=Individual&email=test%40mail.nl&first_name=John&last_name=Doe
Ofcourse this isn't formatted the way the api asks for it, namely:
CURLOPT_POSTFIELDS => "{\r\n \"type\" : \"INDIVIDUAL\",\r\n \"email\" : \"Maria#Papakiriakou.com\",\r\n \"first_name\" : \"Maria\",\r\n \"last_name\" : \"Papakiriakou\"\r\n}",
And also ofcourse, a cURL error appears as follows:
{"id":"xxxx-xxxx-xxxx-xxxx-xxxxxxxxx","type":"malformed_content","message":"Content of the request doesn't conform to specification"}
What do I need to do in my php code so that the API will accept my sent data?
Change the line:
$fields_string = http_build_query($fields);
Into this:
$fields_string = json_encode($fields);
Because the API expecting JSON body, as you are sending non-JSON post body the API will don't know what is it and reject
Why don't you try the response firstly via POSTMAN by setting the data as required by the API and test what actually the API needs.
As per the information provided, it looks like you should remove the below line and just post json_encoded data.
$fields_string = http_build_query($fields);
Related
To secure our passwords and API keys used on our website we use Microsoft Keyvault on Azure. Every time a user visits our website I make a connection to Keyvault and get the passwords and API keys. This means that every refresh of one of our pages of our website executes the following PHP code. This works perfectly and fast, but I would like to reduce the amount of logins to Microsoft Keyvault.
How can I check if the token is still valid for this user without saving the token in a session.
// GET TOKEN
$url = "https://login.microsoftonline.com/GUID/oauth2/v2.0/token";
$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/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "grant_type=client_credentials&client_id=GUID&client_secret=CLIENTSECRET&scope=https://vault.azure.net/.default";
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
$response = json_decode($response, true);
$token = $response['access_token'];
// LOGIN TO KEYVAULT WITH TOKEN
curl_setopt_array($curl, array(
CURLOPT_URL => "https://kv-webservice-westus.vault.azure.net/secrets/".$keyvault_secret."?api-version=7.2",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 25,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$token.''
),
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
$keyvault_secret = $response['value'];
I have trying this all day, but couldn't find a way out.
I have two codes; the first one generates an AccessToken that will be used to call an API. I also parsed the Json output so that, I can have only the AccessToken parameter displayed... Here's the code;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://sandbox.monnify.com/api/v1/auth/login/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Basic TUtfVEVTVF9TQUY3SFI1RjNGOjRTWTZUTkw4Q0szVlBSU0JUSFRSRzJOOFhYRUdDNk5M"
));
$response = curl_exec($ch);
$token = json_decode($response, true);
$at = $token['responseBody'];
echo $at['accessToken'];
curl_close($ch);
The above code outputs the token.
Now, this is the code that sends the request below;
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts/ogmic1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibW9ubmlmeS1wYXltZW50LWVuZ2luZSJdLCJzY29wZSI6WyJwcm9maWxlIl0sImV4cCI6MTYwMTE0MDA0MywiYXV0aG9yaXRpZXMiOlsiTVBFX1JFVFJJRVZFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfREVMRVRFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfUkVUUklFVkVfUkVTRVJWRURfQUNDT1VOVF9UUkFOU0FDVElPTlMiLCJNUEVfSU5JVElBTElaRV9QQVlNRU5UIiwiTVBFX1JFU0VSVkVfQUNDT1VOVCIsIk1QRV9DQU5fUkVUUklFVkVfVFJBTlNBQ1RJT04iXSwianRpIjoiNmQ0OTA0ZDQtMzNjYy00Y2NlLWI3OTgtODg3ZjdlOTEwM2JhIiwiY2xpZW50X2lkIjoiTUtfVEVTVF9TQUY3SFI1RjNGIn0.jqjtn8uyVxSL8oVPNHojMRzqiPx__xYTzoPCsi5IJgiXd184Nc6XSp7QaZlbav6NjXQAt8uuYWVuEqZpJoHJSJlvR-QbpV_pkx582EV0cGdTNzFAUgyrRYvWxmFmNeS-XBVk0-8labvDqyLHcpjfoT_FaiBylY2ek99G8WvXxnvLE1STY91blhMb3UCKBykDzVlmX0U6gvWd9Btgb1-Zb18Olt-GHmsYavBDSvSpptSiIleFPsZzL1O_u366DVMmGrCq-1iqYpBBRixU0uslTsp17VJfvHJUwciPSHDDaPK2iH6QP7Nv4jPYMNhs8MW7L67NYO9yohD9qfAOTyWv6g"
),
));
$response = curl_exec($curl);
$objs = json_decode($response, true);
$rp = $objs['responseBody'];
echo "<div class='dlmenu1'> <span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;font-weight:bold;padding:2px;margin-right:3px;'>" .
$rp['accountNumber'] . "</span>";
echo "<span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;font-weight:bold;padding:2px;margin-right:3px;'>(" . $rp['bankName'] . ")</span>";
echo "<span style='background:#fff;color:#295f2d;font-size:13px;text-align:center;padding:2px;margin-right:3px;'>" . " - Instant Funding</span></div>";
curl_close($curl);
I need help on how I can pass the AccessToken generated in the first code to the second one (the two codes combined), so that it can serve as the Authorization Bearer.
I don't mind if the two codes can be combined.
Thanks
I tested an API using postman. Using, postman, I managed to generate a token.
From postman, I managed to get all the data of a user using the following with the following details via a GET request:
https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com
Under Authorization type, I selected "Bearer token" and pasted the Token in its respective field.
When I click send, I get a success response with the user data.
In PHP, how can I do the same api call (using the id,email and token) inside a php function?
I tried this:
curl_setopt_array($curl, array(
CURLOPT_URL => "example.example.co.za/api/consumers/get?email=example#sample.com&id=567675675",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer jhgukfytjfytdytfjgjyfytfjkugfyfdhtklhkugjf",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
But when I view the page, instead of the user data, I see this:
How do I make the API call inside a function to get the same result that I have on postman in PHP?
You need to follow the location
CURLOPT_FOLLOWLOCATION => true
https://www.php.net/manual/de/function.curl-setopt.php
Please find a proper CURL call here.
$url = "https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com";
$curl = curl_init();
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
I am using Office365 To Sync Email in PHP. The Office 365 REST API supports batch requests. I have tried to use this to retrieve emails using the Office 365 REST API, but so far I receive a "BadRequest." reply. Any help would be appreciated. Here is the sample code
$url = 'https://graph.microsoft.com/v1.0/$batch';
$headers = array(
"Authorization: Bearer ".$accessToken,
"Content-Type: application/json"
);
$msgid1 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtMDACLTAPwAAAHzY91EAAAA=";
$msgid2 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtDACLTvpPwAAAHzY91AAAAA=";
$msgid3 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTRRRBGUtMDACLAAHzY908AAAA=";
$params = array(
'requests' => array(
array(
"id" => "1",
"method" => "GET",
"url" => "/me/messages/$msgid1/attachments"
),
array(
"id" => "2",
"method" => "GET",
"url" => "/me/messages/$msgid2/attachments"
),
array(
"id" => "3",
"method" => "GET",
"url" => "/me/messages/$msgid3/attachments"
)
)
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
In Response i receive Invalid batch payload format message.
Oh.. There is error in my code.
Do
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
insted of
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
I want to fetch content from my Atlassian with username and password.
The URL typically looks like:
http://my-own-site.atlassian.net/wiki/pages/viewpage.action?spaceKey=TO&title=Any-Wiki-Title
Is it possible to use PHP CURL to fetch content from this page?
So far I am only getting 401 auth reqd error.
I have looked through Stackoverflow and all I am getting is how to access basic atlassian.com and bitbucket.org pages.
With this code in php, you can create Confluence Pages:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"**inserttitle**\",\"space\":{\"key\":\"**insertspace**\"},\"ancestors\":[{\"type\":\"page\",\"id\":**insertancestor**}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
And with this code, you can get content from Confluence:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/**insertid**");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "**insertusername**" . ":" . "**insertpassword**");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
echo $result;
Modify the parameter. I marked the words with insert*
Yes, it is certainly possible to access Atlassian products using PHP and cURL. I do it all the time to create/modify Jira issues
You will have to find/write a library (or set of libraries) which will allow you to access REST API calls. In my case, I wrote a base REST library which can then be inherited to create Jira, Confluence, any other REST service libraries
Search the Atlassian documentation site to find the REST API for the product you're using (Confluence in your case I would guess)
Don't forget that the REST API uses GET, POST, PUT and DELETE methods so your library will need to handle all of these
With regards to your error, I *think* your login will need to be allowed access to the API calls
To retrieve any existing content properties for a piece of content, use url as https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}
$curl = curl_init();
$post = array(
"id" => "{content_ID}",
"type" => "{content_ID}", //Ex. page
"title" => "{content_Title}",
"space" => ["key" => "{spaces_key}"],
"body" => ["storage" => ["value" => "<p>Here comes the other text</p>", "representation" => "storage"]],
"version" => ["number" => 15]
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://your-domain.atlassian.net/wiki/rest/api/content/{content_ID}?expand=metadata.properties.myprop,space,body.view,version,container",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 300,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_HTTPHEADER => array(
"authorization: Basic {base64 of (username:password)}",
"content-type: application/json",
'Accept: application/json'
),
));
$result = curl_exec($curl);
curl_close($curl);