I am trying to send a message to Discord Webhook, with Components.
I have tried normal message and embed, and both give me the same result. I get the message, but not the components.
Syntax From here: https://discord.com/developers/docs/interactions/message-components
Here is my code:
<?php
$url = "https://discord.com/api/webhooks/1234567890";
$content = '{
"content": "This is a message with components.",
"components": [
{
"type": 1,
"components": [
{
"type": 2,
"label": "Click me!",
"style": 1,
"custom_id": "click_one"
}
]
}
]
}';
$curl = curl_init();
$headers = array('Content-Type: application/json');
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $content,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => true
));
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($response);
Here is the var_dump of the response:
Related
I want to create envelope using DocuSign API in my web application. When I run it in postman then response showing object move with html content
This is my php code sample
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://account-d.docusign.com/v2.1/accounts/b15b77b0-9345-4780-bfc1-440b37991820/envelopes?change_routing_order=true',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"documents": [
{
"documentBase64": "Base64 code of my pdf file",
"documentId": "5865888",
"fileExtension": "pdf",
"name": "test pdf"
}
],
"emailSubject": "test pdf",
"recipients": {
"signers": [
{
"name": "User full name",
"email": "user#gmail.com",
"recipientId": "8959555"
}
]
},
"status": "send "
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: [{"key":"Authorization","value":"Bearer {{accessToken}}"}]'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
My API response is
Object moved to here.
Please guide me how to solve the issue
Thank You
That base URL is incorrect. Account-d.docusign.com is only for authentication.
Requests should be made to {SERVER}/restapi/v2.1
So for you it would be demo.docusign.net/restapi/v2.1/accounts/{ACCOUNT_ID/envelopes
Here's the dev centre article showing the whole process
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have tried getting a response from my request but it didn't work.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox.monnify.com/api/v1/bank-transfer/reserved-accounts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n \"accountReference\": \"0111443-JP098\",\n \"accountName\": \"Joe Philips\",\n \"currencyCode\": \"NGN\",\n \"contractCode\": \"6146592431\",\n \"customerEmail\": \"jp#tester.com\",\n \"customerName\": \"Joe Philips\",\n \"incomeSplitConfig\": [\n {\n \"subAccountCode\": \"MFY_SUB_275274693326\",\n \"feePercentage\": 10.5,\n \"splitPercentage\": 20,\n \"feeBearer\": true\n }\n ]\n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibW9ubmlmeS1wYXltZW50LWVuZ2luZSJdLCJzY29wZSI6WyJwcm9maWxlIl0sImV4cCI6MTU5NzI2MzAyNCwiYXV0aG9yaXRpZXMiOlsiTVBFX01BTkFHRV9MSU1JVF9QUk9GSUxFIiwiTVBFX1VQREFURV9SRVNFUlZFRF9BQ0NPVU5UIiwiTVBFX0lOSVRJQUxJWkVfUEFZTUVOVCIsIk1QRV9SRVNFUlZFX0FDQ09VTlQiLCJNUEVfQ0FOX1JFVFJJRVZFX1RSQU5TQUNUSU9OIiwiTVBFX1JFVFJJRVZFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfREVMRVRFX1JFU0VSVkVEX0FDQ09VTlQiLCJNUEVfUkVUUklFVkVfUkVTRVJWRURfQUNDT1VOVF9UUkFOU0FDVElPTlMiXSwianRpIjoiYTQ5YjIxNDgtNTJkMy00ZGI1LTg2NGYtYzdiM2NjM2M4NzUzIiwiY2xpZW50X2lkIjoiTUtfVEVTVF9DSFZRRlJBN1NHIn0.mqwi5y7wnXpBCk6R9dC3ORhf9pNkwHVOCJr2SHUfk9TFYpVMnGuBUVyxJFOwLHROyKVodquPr1eS2AT1nTUDCrW0YXlX9tX5BPrfckvDoPPza7Klc8uQrw1aVxF6sAK-hFZgC79lKOq9gowOqWP1frbJ5BqozZfYiQ6ZsZcf2LubDOoen_G6_13wGtCM58-9BcY6aMKv--Vxr0AFwSqujBMny1D-x2SgsqT98asoYvtaHGtiC4MbVg-jFwwJuG4BYststO0k1J0YI5frpyLyQfaNEJSR6Y-WJiCqFWIDpHgDFINl65xtMIE_15OV2BKfBsruCo5mkx_rHPH_4_tXWw"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
These is the request code in curl, and below is the response
{
"requestSuccessful": true,
"responseMessage": "success",
"responseCode": "0",
"responseBody": {
"contractCode": "6146592431",
"accountReference": "abc123",
"accountName": "Test Reserved Account",
"currencyCode": "NGN",
"customerEmail": "test#tester.com",
"accountNumber": "3000004533",
"bankName": "Providus Bank",
"bankCode": "101",
"collectionChannel": "RESERVED_ACCOUNT",
"reservationReference": "W3HP7E6VZP78TUKV4U9E",
"reservedAccountType": "GENERAL",
"status": "ACTIVE",
"createdOn": "2020-08-12 18:56:49.0",
"contract": {
"name": "Default Contract",
"code": "6146592431",
"description": null,
"supportsAdvancedSettlementAccountSelection": false,
"sweepToExternalAccount": false
},
"transactionCount": 0,
"restrictPaymentSource": false
}
}
In all of the response body, I need just the following response;
"accountNumber": "3000004533",
"customerEmail": "test#tester.com",
"bankName": "Providus Bank",
Please, how do I get this done.
Parse json string to PHP object or array and then you get it. Get your info to $rp variable
...
$response = curl_exec($curl);
$obj = json_decode($response, true);
$i = 0;
$nw= array_filter($obj['responseBody'], function($v,$k){
return in_array($k, ['accountNumber', 'customerEmail', 'bankName']);
}, ARRAY_FILTER_USE_BOTH);
$i=0;
foreach($nw as $k => $v){
$last = $i !== count($nw)-1 ? ','.PHP_EOL : '';
echo "\"$k\": \"$v\"".$last;
$i++;
}
curl_close($curl);
EDIT: Just show the result in the
Today I was testing PATCH calls to an API using PHP for the first time and to this moment I have not quite figured how to make them work.
I want to add a new parameter with it's value to an already existing set of parameters, I have set CURLOPT_CUSTOMREQUEST => "PATCH" as option and "Content-Type: application/json-patch+json" as the proper header. Despite my body being an array as the error first indicated when I parsed it into JSON, now it says that the entity is still not a well-formed application/json-patch+json.
I am totally lost as to why this could be, since other examples I have seen do not differ from what I wrote.
This is the existing set of parameters:
{
"ac": "774",
"allowed_clis": [
"34774554114"
],
"cc": "34",
"cli": "34774554114",
"id": 1512,
"subscriber_id": 1512
}
What I am trying to achieve:
{
"ac": "774",
"allowed_clis": [
"34774554114"
],
"cc": "34",
"cli": "34774554114",
"id": 1512,
"e164_to_ruri": true,
"subscriber_id": 1512
}
This is my code:
$dataArray = [
'op' => 'add',
'path' => '/e164_to_ruri',
'value' => true
];
function setPreferences($dataArray, $uri, $token){
$ch= curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => $dataArray,
CURLOPT_URL => $uri,
CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);
And finally, the response I get:
Array
(
[message] => The entity is not a well-formed 'application/json-patch+json' document. Malformed JSON: Expected array or object at line 0, offset 0
[code] => 400
)
To my limited knoledge, $dataArray is a well-formed array. So I don't understand where is this response coming from.
Thank you so much for the help provided!
As #UlrichEckhardt pointed out, all I needed to do is turn my data into a JSON array of JSON objects:
function setPreferences($uri, $token){
$ch= curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => '[{"op": "add", "path": "/e164_to_ruri", "value":true}]',
CURLOPT_URL => $uri,
CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);
It doesn't work and responds with
{"message": "Cannot send an empty message", "code": 50006}
I've tried entering the message into CURLOPT_POSTFIELDS
<?php
$url = 'https://discordapp.com/api/channels/638164827180630046/messages';
$ch = curl_init();
$payload = json_encode( array( "content"=> "a", "nonce"=> "638201106664521728", "tts"=> false ) );
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: Bot <token>'),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => "a"
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
echo $response;
?>
I want it to send a message
I have been trying to use cURL to interact with an api, I want to download a file with this snippet but all it does is open up the file in the browser and the second snippet is supposed to upload a pdf file along with multipart form data with cURL, but all it give me is;
"{"type":"request_error","detail":"Multipart form parse error - Invalid boundary in multipart: None"}"
error, I have already tried to execute the thing with postman, it runs fine there and when i copy and paste the same code it still gives me the same errors as above, cheers, thanks in advance.
Here is the code;
Downloading Snippet
$id = "rZFbMKmj2zZbMUbp5KrjCH";
$auth = $this->access_token;
$curl = curl_init( PANDA_APIURL . "/documents/".$id."/download");
//$fp = fopen('Test.pdf', 'w+');
curl_setopt_array($curl, array(
CURLOPT_URL => PANDA_APIURL . "/documents/".$id."/download",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 50,
CURLOPT_TIMEOUT => 300,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//CURLOPT_FILE => $fp,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $auth"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
//fclose($fp);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Uploading the file with multipart form data snippet;
$auth = $this->access_token;
$boundary = '7aBMjcE3CIYntqQ3';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => PANDA_APIURL . "/documents",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '------WebKitFormBoundary7aBMjcE3CIYntqQ3
Content-Disposition: form-data; name="file"; filename="samplepdf.pdf"
Content-Type: application/pdf
------WebKitFormBoundary7aBMjcE3CIYntqQ3
Content-Disposition: form-data; name="data"
{
"name": "Pdf Doc",
"recipients": [
{
"email": "xyz#gmail.com",
"first_name": "Jane",
"last_name": "Roe"
}
],
"fields": {
"name": {
"value": "John"
},
"like": {
"value": true
}
}
}
------WebKitFormBoundary7aBMjcE3CIYntqQ3',
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $auth",
"Content-Type: multipart/form-data;----WebKitFormBoundary'.$boundary"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}