Freshdesk API: Attach dataurl as file using PHP and CURL - php

I'm trying to create a ticket in Freshdesk with a screenshot as an attachment. Screenshot is captured in a canvas and converted to dataurl. And I'm using the following code to pass it to the Freshdesk API (V2)
$data = [
"description" => $messageBody,
"subject" => 'Bug Report from Client',
"email" => $replyTo,
"priority" => 1,
"status" => 2,
"attachments" => [
[
"type" => "file",
"name" => "Screenshot",
"content-type" => "image/png",
"resource" => $dataUrl,
]
]
];
$url = "https://domain.freshdesk.com/api/v2/tickets";
$apiKey = "key";
$headers = array(
'Content-Type:application/json',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
$resultStr = curl_exec($ch);
It results in the following error.
{
field: "attachments",
message: "It should contain elements of type valid file format only",
code: "datatype_mismatch"
}
Without the attachments field, the above code works fine. How do I fix this?

Related

How to Create Shopify Order Via Api using Php?

I am trying to Create a Shopify Order using Api this is my code :
$arrOrder= array(
"email"=> "foo#example.com",
"fulfillment_status"=> "fulfilled",
"send_receipt"=> true,
"send_fulfillment_receipt"=> true,
"line_items"=> array(
array(
"product_id"=>875744960642,
"variant_id"=> 3558448932592,
"quantity"=> 1
)
),
"customer"=> array(
"id"=> 458297751235
),
"financial_status"=> "pending"
); echo json_encode($arrOrder);
echo "<br />";
$url = "https://AkiKey:Password#Store.myshopify.com/admin/api/2021-01/orders.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arrOrder));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r($response);
and the response is :
{"errors":{"order":"Required parameter missing or invalid"}}
I think there is some data mismatch that is sent using API Call, according to the documentation this the format to create an order.
So I think your request is something like this one demo code
$arrOrder= [
"order" =>[
"email" => "foo#example.com",
"fulfillment_status" => "fulfilled",
"send_receipt" => true,
"send_fulfillment_receipt" => true,
"line_items" => [
[
"product_id" => 875744960642,
"variant_id" => 3558448932592,
"quantity" => 1
]
],
"customer" => [
"id"=> 458297751235
],
"financial_status"=> "pending"
]
];

Batch Request in office365 php gives Bad Request

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);

Http 400 error when creating new Mailchimp list

I am trying to create a new list via the mailchimp api, I have tried tweaking the code so much but keep getting the error:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"0174d737-b3d4-40a6-9cd4-e934ed8578a7","errors":[{"
field":"","message":"Schema describes object, NULL found instead"}]}
My code (api key is valid):
$data = array( // the information for your new list--not all is required
"name" => $name,
"contact" => array (
"company" => $company,
"address1" => $address1
),
"permission_reminder" => $permission_reminder,
"use_archive_bar" => $archive_bars,
"campaign_defaults" => array(
"from_name" => $from_name,
"from_email" => $from_email,
"subject" => $subject,
"language" => $language
),
"notify_on_subscribe" => $notify_subs,
"notify_on_unsubscribe" => $notify_unsubs,
"email_type_option" => $type,
"visibility" => $visibility
);
$ch = curl_init("https://$dataCenter.api.mailchimp.com/3.0/lists/");
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result); // display API response
I'm not seeing any JSON encoding of your payload in the code you provided. You will need to json_encode() your payload ($data) in order for MailChimp to be able to read it.
Like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Paymaya integration in PHP

im trying to create a customer in paymaya using curl in php.
im following this documentation http://developers.paymaya.com.payment-vault.s3-website-ap-southeast-1.amazonaws.com/#card-vault-customers-post
but its not returning the right response(it returns nothing)
<?php
require_once(DIR_VENDOR . 'PayMaya-PHP-SDK-master/sample/autoload.php');
Class Paymaya {
public function paymayaInit(){
PayMayaSDK::getInstance()->initCheckout("pk-nRO7clSfJrojuRmShqRbihKPLdGeCnb9wiIWF8meJE9", "sk-jZK0i8yZ30ph8xQSWlNsF9AMWfGOd3BaxJjQ2CDCCZb", "SANDBOX");
}
public function createCustomer(){
$this->paymayaInit();
// $ch = curl_init("https://pg-sandbox.paymaya.com/payments/v1/customers");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
$response = curl_exec($ch);
curl_close($ch);
// die(print_r($response));
return json_decode($response);
}
}
here is my class.
does anyone already tried integrating paymaya in php?
also i have to comment the namespace in PayMayaSDK.php to be able to use PayMayaSDK class
Thanks for your credentials posted in question I made few requests and found what was wrong.
First of all I used curl_error($ch) to find out what error was.
It's The requested URL returned error: 400 Bad Request.
Problem is that you set Content-Type to be json, but sending URL encoded query.
Change http_build_query to json_encode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
echo __FILE__."#".__LINE__."<pre>";
var_dump($response, curl_error($ch));
echo "</pre>";
curl_close($ch);

Google Cloud Speech API using php

I am trying to call Google Cloud Speech API by using PHP and got a problem.
$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
$upload = file_get_contents("1.wav");
$upload = base64_encode($upload);
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sampleRate" => 16000,
"languageCode" => "en-US"
),
"audio" => array(
"Content" => $upload,
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
The result says that it is INVALID JSON PAYLOAD.
{ "error": { "code": 400, "message": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field.", "status":
"INVALID_ARGUMENT", "details": [ { "#type":
"type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ {
"field": "audio", "description": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field." } ] } ] } } "
I think this is because $upload isn't configured correctly.
According Google Cloud Speech API, it should be "A base64-encoded string".
https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
That's why I used base64_encode function, but it seems JSON doesn't process this value correctly.
Any thoughts?
You need to construct the properly formatted input as an array and then json encode it. For example, to send a file, base64encode it as "content" and submit to the API as shown:
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sample_rate" => $bitRate,
"language_code" => "en-IN"
),
"audio" => array(
"content" => base64_encode($filedata)
)
);
$data_string = json_encode($data);
$ch = curl_init($googlespeechURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
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);
$result_array = json_decode($result, true);
please make 'content' instead of 'Content'
small letter 'c'
its work for me.

Categories