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);
Related
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?
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"
]
];
Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);
I want to create product in shopify through api.I tried with below code,but its not working.
<?php
$products_array = array(
"product" => array(
"title" => "Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$SHOPIFY_API = "https://apikey:password#domainname.myshopify.com/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array( "Authorization: Basic ".base64_encode("apikey:password"),
"Content-Type: application/json",
"charset: utf-8");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
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_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
It gives response as '{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}'.any idea?
Please check if there is write permission is set or not for products in your private app.
I have found the issue, it's not about permission, is about the URL of the API you are posting it wrong URL. Here is the right one:
https://{apikey}:{password}#{hostname}/admin/api/{version}/{resource}.json
And here is the code:
<?php
$products_array = array(
"product" => array(
"title" => "New Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$API_KEY = 'apikey';
$PASSWORD = 'password';
$SHOP_URL = 'domainname.myshopify.com';
$SHOPIFY_API = "https://$API_KEY:$PASSWORD#$SHOP_URL/admin/api/2020-04/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array(
"Authorization: Basic ".base64_encode("$API_KEY:$PASSWORD"),
"Content-Type: application/json",
"charset: utf-8"
);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
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_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
try "published" => false ,
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);