Insert event to Google Calendar using php - php

I'm trying to perform a cURL request to google calendar api using their guide, that says:
POST https://www.googleapis.com/calendar/v3/calendars/{name_of_my_calendar}/events?sendNotifications=true&pp=1&key={YOUR_API_KEY}
Content-Type: application/json
Authorization: OAuth 1/SuypHO0rNsURWvMXQ559Mfm9Vbd4zWvVQ8UIR76nlJ0
X-JavaScript-User-Agent: Google APIs Explorer
{
"start": {
"dateTime": "2012-06-03T10:00:00.000-07:00"
},
"end": {
"dateTime": "2012-06-03T10:20:00.000-07:00"
},
"summary": "my_summary",
"description": "my_description"
}
How am I supposed to do that in php? I wonder what parameters I should send and what constants I should use. I'm currently doing:
$url = "https://www.googleapis.com/calendar/v3/calendars/".urlencode('{name_of_my_calendar}')."/events?sendNotifications=true&pp=1&key={my_api_key}";
$post_data = array(
"start" => array("dateTime" => "2012-06-01T10:00:00.000-07:00"),
"end" => array("dateTime" => "2012-06-01T10:40:00.000-07:00"),
"summary" => "my_summary",
"description" => "my_description"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// adding the post variables to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
but the response is:
{
error: {
errors: [
{
domain: "global",
reason: "required",
message: "Login Required",
locationType: "header",
location: "Authorization"
}
],
code: 401,
message: "Login Required"
}
}
How should I format my parameters?

I noticed this question is asked quite some time ago, however after figuring out the post-parameter problem after some time I thought it might be useful to others to answer it. First within '$post_data', I switched the 'start' and 'end':
$post_data = array(
"end" => array("dateTime" => "2012-06-01T10:40:00.000-07:00"),
"start" => array("dateTime" => "2012-06-01T10:00:00.000-07:00"),
"summary" => "my_summary",
"description" => "my_description"
);
Secondly, I figured Google Calendar API expected the data to be json, so in curl_setopt:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
This worked perfectly for me, hope it's useful to someone else as well!

Related

Sage Accounting API UnexpectedError

I am trying to create a Sales Invoice through Sage Accounting API calls (its documentation can be found here: https://developer.sage.com/api/accounting/api/)
To make my code clearer I have created a class that helps me make those calls accordingly.
Here is the method I use to make those calls:
public function postRequest()
{
$url = $this->baseEndpoint . $this->request;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (isset($this->params)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $this->token",
"Host: api.accounting.sage.com",
"Content-Type: application/json"
));
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
How I call this method:
$params = array(
"sales_invoice" => array(
"contact_id" => "485fdfe0be154f9c9af44351de16e5be",
"date" => "2019-06-13",
"invoice_lines" => array(
array(
"description" => "description",
"ledger_account_id" => "f04157c90ff0496ab3a22f2558e46010",
"unit_price" => 10 ,
"quantity" => 1,
"tax_rate_id" => "ES_RE_STANDARD",
"tax_rate" => 0.1
)
)
)
);
$params = json_encode($params);
$request = "v3.1/sales_invoices";
$sageRequest = new SageRequest($token, $request, $params);
$sageRequest->postRequest();
According to the API documentation, that should work, but still I get this error:
[$severity] => error
[$dataCode] => UnexpectedError
[$message] => An unexpected error occurred.
[$source] =>
If there is anyone who has some experience with the Sage Accounting API, I would be more than grateful to know what I have done wrong.
This example works for me on a Spanish business:
{
"sales_invoice": {
"contact_id": "22b609fba11642238f2ecd0f5fe3e0b5",
"date": "2019-06-12",
"invoice_lines": [
{
"description": "Description",
"ledger_account_id": "829739738de811e996c90122ae3d08ca",
"quantity": 1,
"unit_price": 100,
"tax_rate_id": "ES_STANDARD"
}
],
"main_address": {
"city": "Madrid"
}
}
}
Make sure your contact is listed in the contact endpoint. Use GET https://api.accounting.sage.com/v3.1/ledger_accounts?visible_in=sales to get a list of all valid ledger accounts for sales objects.
I see your question uses ES_RE_STANDARD as tax rate. I will update this answer soon with an example for the "recargo de equivalencia" tax rate.

Stripe API create SKU

I need to create a SKU via stripe API.
The problem is in inventory field.
Stripe api response is:
'error' => [
'message' => 'Invalid hash',
'param' => 'inventory',
'type' => 'invalid_request_error'
]
My php code is:
$endPoint = 'https://api.stripe.com/v1/skus';
$APIKEY_TEST = 'my_api_key';
$headers = array('Authorization: Bearer '.$APIKEY_TEST);
$sku = [
'active' => 'true',
'inventory' => ['quantity' => 10000000 ,'type' => 'infinite', 'value' => null],
"currency" => "eur",
"price" => $price,
"product" => $stripe_product_id
];
$array_string ='';
foreach($sku as $key => $value) {
$array_string .= $key.'='.$value.'&';
}
rtrim($array_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
In stripe api docs inventory is hash type field.
I have tried json_encode() without luck.
Maybe the problem is in sending an array instead of a hash.
In $sku array, inventory field is also an nested associative array.
Maybe the problem resides there as well.
Is there a way to send CURLOPT_POSTFIELDS containing inventory so that stripe accepts it?
EDIT:
In Stripe dashboard i can see my request:
{
"active": "true",
"inventory": "Array",
"currency": "eur",
"price": "3",
"product": "prod_F6ipvfYFvOxxQq"
}
Inventory field has no data, but instead "Array".
After trying a lot of possible solutions, i found the answer:
$post_array = http_build_query($sku);
And know stripe accepts the $sku array with nested inventory array.
It worth notice that stripe does not accept JSON in requests.
The request has to be url encoded.

Php Magento Api Rest Create Customer Password Issue :

I'm using the Magento ver. 2.1.2 Rest Api to create users, following this :
http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers
$data = [
"customer" => [
"firstname" => 'Earl',
"lastname" => 'Hickey',
"email" => 'earl-2#example.com',
"password" => 'password',
"website_id" => 1,
'store_id' => 1,
"group_id" => 1
]
];
$token = $this->get('lp_api')->getToken();
$ch = curl_init( $this->endpoint . 'customers');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json", "Authorization: Bearer " . json_decode( $token ),
)
);
// var_dump(curl_getinfo($c));
$result = curl_exec($ch);
If i send a password (as in the example above), i've got the following error :
Next Exception: Report ID: webapi-583357a3bf02f; Message: Property "Password" does not have corresponding setter in class "Magento\Customer\Api\Data\CustomerInterface". in /var/www/html/www.magento.dev/vendor/magento/framework/Webapi/ErrorProcessor.php:195
I noticed that if i remove the "password" => 'password' from the $data array, a user is created without password (seems odd to me).
I can't find any help on this error.
Any idea anyone ?
Refer below link for Magento 2.x version.
http://devdocs.magento.com/swagger/index_20.html#/
I have used below body for creating customers through Rest Api and it worked properly.
{
"customer": {
"email": "xyz#abc.com",
"firstname": "x",
"lastname": "y",
"website_id":1,
"group_id":1,
"custom_attributes": [
{
"attribute_code": "mobile_no",
"value": "1234567890"
}
]
},
"password": "123456"
}

Pinterest API not working as expected

I'm trying to use the Pinterest API using PHP, but so far it doesn't really work.
I use this guide to get started:
https://developers.pinterest.com/docs/api/authentication/
I got everything working now except for step 3.
The documentation says it's just a simple request to
https://api.pinterest.com/v1/me/?access_token=<YOUR-ACCESS-TOKEN>
Making authenticated requests
Finally, once you have an access token, you can make authorized
requests on the users behalf using OAuth 2.0 Bearer tokens supplied
via the Authorization request header or via a request parameter named
access_token (supplied as either a form-encoded body parameter or a
URI query parameter). The Authorization header is preferred.
For example, you can visit this site on your browser:
https://api.pinterest.com/v1/me/?access_token= A
sample response might look like:
{
"data": {
"url": "https://www.pinterest.com/ben/",
"first_name": "Ben",
"last_name": "Silbermann",
"id": "4788400174839062"
} }
I have an access_token which looks like this:
AcFFayZKwqXuKVkj1J-0QN1fCob1FAgjuP9-OtFCg_j49UAgogXXXXX
But everytime I get this message:
{"status": "failure", "code": 3, "host": "coreapp-devplatform-devapi-181", "generated_at": "Mon, 28 Sep 2015 12:28:38 +0000", "message": "Authorization failed.", "data": null}
I use this PHP code to get data from the URL:
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded"),
CURLOPT_NOBODY => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => self::userAgent()
));
$result = curl_exec($curl);
var_dump($result);
$errorCode = curl_errno($curl);
$respCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
I just gone through this same error, but for different endpoint. Try this code
$ch = curl_init();
$url = "https://api.pinterest.com/v1/me/?access_token='YOUR_ACCESS_TOKEN'";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = trim(curl_exec($ch));
curl_close($ch);
print_r($result);
Please have a look at this discussion. It covers the exact same issue you are describing. As far as I can tell right now it seems to be an issue on Pinterest's side. #ZackArgyle from Pinterest's dev team got notified and is working on it I believe.

PHP JSON Request

Hi I am trying to make JSON Request to a API but I am not sure how to as I have never worked on something similar ever before. I would really appreciate if someone can help me please.
Below is the request that I need to make:
request payload:
{
"sessionId": "1234567890",
"availabilityRequest": {
"checkInDate": "29042014",
"checkOutDate": "",
"noRooms": 1,
"noNights": 1,
"userType": "leisure",
"rateType": "standard",
"roomPreference": [
{
"noAdult": 1,
"noChild": 0
}
],
"siteCode": [
"GB0758",
"GB0746",
"GB0738",
"GB0755",
"GB0742"
],
"includeDisabled": "F"
}
}
This is what I have done but I am getting error Array ( [error] => Array ( [code] => 4007 [message] => Invalid JSON POST data (unable to decode): ) )
$postData = '{
"sessionId":"1234567890",
"availabilityRequest":
{
"checkInDate": "29042014",
"checkOutDate": "",
"noRooms": 1,
"noNights": 1,
"userType": "leisure",
"rateType": "standard",
"roomPreference":
[
{ "noAdult":1, "noChild":0 }
],
"siteCode":
[
"GB0758","GB0746","GB0738","GB0755","GB0742"
],
"includeDisabled":"F"
}
}';
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => $postData));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
print_r($responseData);
I would be really grateful is someone can help me please. Thank you
Your JSON is invalid. Validate it using a tool such as http://jsonlint.com/
You should add an other } to the end to close it. Then you will have a valid JSON.
I can't really test this because I have no URL to test it with, but you can try to set a header.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postData)));

Categories