Can't insert multiple events at once in Google Calendar in php - php

I'm trying to insert multiple events to the google calendar with PHP. When I try to insert one event with this code below, it works, but when I want to insert multiple events on google calendar API server, to save more data, i get some error.
This is the code that I have:
I have an JSON string, this string was converted to a php array.
$calendar =
json_decode('
{
"summary": "Calendar name",
"description": "Calendar description",
"items":
[
{
"summary": "event1",
"colorId": "10",
"start": {
"dateTime": "2018-05-10T12:30:00+02:00"
},
"end": {
"dateTime": "2018-05-10T13:30:00+02:00"
},
"recurrence": [
"RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=TH"
]
},
{
"summary": "event2",
"start": {
"dateTime": "2018-05-17T12:30:00+02:00"
},
"end": {
"dateTime": "2018-05-17T13:30:00+02:00"
}
},
{
"summary": "event3",
"description": "event3 description",
"colorId": "2",
"start": {
"date": "2018-05-10"
},
"end": {
"date": "2018-05-11"
},
"transparency": "transparent"
}
]
}
', true);
With this functions, I can add only one event to google calendar. When I try to insert more events I get errors...
function CreateCalendarEvents($calendar_id, $access_token, $events_json) {
$url_events = 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_events);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($events_json));
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200){
throw new Exception('Error : Failed to create events');
}
return $data['id'];}
I call function with this line of code:
CreateCalendarEvents('primary', $_SESSION['access_token'], $calendar);

Related

Cannot post data to API via Curl and PHP

Am working with Blinksky Payments API.
Below is the post parameters
URI https://api.blinksky.com/api/v1
POST /send
{
"gift": {
"action": "order",
"apikey": "{APIKey}",
"sender": "Athens Automotive Inc.",
"from": "17705551234",
"dest": "16150001234",
"code": "62",
"amount": 100,
"postal": "30005",
"msg": "Thanks for taking our test drive!",
"reference": "{your_unique_order_id}",
"handle_delivery": false
}
}
When I tried my code below, it throws error
{ "statusCode": 404, "message": "Resource not found" }
What am I doing wrong? Below is the code:
$url = "https://api.blinksky.com/api/v1";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$post ='{
"gift": {
"action": "order",
"apikey": "my-api-key-goes-here",
"sender": "Luxury Automotive Inc.",
"from": "17705551xxxxxxx",
"dest": "16150001xxxxxxx",
"code": "62",
"amount": 100,
"postal": "90211",
"msg": "Thanks for the Gift!",
"reference": "G62-786",
"handle_delivery": false
}
}';
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'
));
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
print_r($response);
You are missing the /send path in your url.
Change this:
$url = "https://api.blinksky.com/api/v1";
to this:
$url = "https://api.blinksky.com/api/v1/send";

Creating the issue with assigning the customer as the reporter with Jira API?

I would like to create new customer if it does not exist and create new issue setting this customer as reporter in PHP with Jira API.
I create new customer using API ‘servicedeskapi/customer’ with POST fields ‘fullName’ and ‘email’ and then I create new issue using API ‘api/2/issue’ with parameters ‘fullName’ and ‘email’ of parameter ‘reporter’.
I am trying do this in this way:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
$postvars = array('fields' => array('project' => array('key' => 'AP'), 'summary' => $body, 'description' => $body, 'reporter' => null));
$postvars['fields']['issuetype'] = array("id" => '10108');
$postvars['fields']['reporter']['fullName'] = $name; //POST field of customer's name to set customer as the reporter of new issue
$postvars['fields']['reporter']['email'] = $email; //POST field of customer's email address to set customer as the reporter of new issue
curl_setopt($ch, CURLOPT_URL, 'https://jira-address/rest/servicedeskapi/customer');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'X-ExperimentalApi: opt-in'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array('fullName' => $reporterName, 'email' => $email))); //POST fields for creating new customer
$r = curl_exec($ch); //sending request for creating new customer
echo json_encode(json_decode($r), JSON_PRETTY_PRINT); //printing response of request for creating new customer
curl_setopt($ch, CURLOPT_URL, 'https://jira-address/rest/api/2/issue/');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postvars));
$r = curl_exec($ch); //sending request for creating new issue
echo json_encode($postvars, JSON_PRETTY_PRINT); //printing POST fields of the request of creating new issue
echo json_encode(json_decode($r), JSON_PRETTY_PRINT); //printing the response of the request for creating new issue
If the customer does not exist, the response is:
{
"name": "kamilszmit2#live.com",
"key": "kamilszmit2#live.com",
"emailAddress": "kamilszmit2#live.com",
"displayName": "kamilszmit2",
"active": true,
"timeZone": "Europe\/Warsaw",
"_links": {
"jiraRest": "https:\/\/jira-address\/rest\/api\/2\/user?username=kamilszmit2%40live.com",
"avatarUrls": {
"48x48": "https:\/\/jira-address\/secure\/useravatar?avatarId=10122",
"24x24": "https:\/\/jira-address\/secure\/useravatar?size=small&avatarId=10122",
"16x16": "https:\/\/jira-address\/secure\/useravatar?size=xsmall&avatarId=10122",
"32x32": "https:\/\/jira-address\/secure\/useravatar?size=medium&avatarId=10122"
},
"self": "https:\/\/jira-address\/rest\/api\/2\/user?username=kamilszmit2%40live.com"
}
}{
"fields": {
"project": {
"key": "AP"
},
"summary": "test",
"description": "test",
"reporter": {
"fullName": "kamilszmit2",
"email": "kamilszmit2#live.com"
},
"issuetype": {
"id": "10108"
}
}
}{
"id": "11187",
"key": "AP-351",
"self": "https:\/\/jira-address\/rest\/api\/2\/issue\/11187"
}
The customer and the issue are created but the customer is not set as reporter of the issue. The same problem occurs if customer already exists and when only the parameter ‘fullName’ or ‘email’ of the parameter ‘reporter’ is used.
What am I doing wrong? How to create Jira issue with the customer as the reporter? Could you help me?
via REST, I have managed to set the reporter by using the name parameter, instead of fullname.
"fields": {
"project": {
"key": "AP"
},
"summary": "test",
"description": "test",
"reporter": {
"name": "kamilszmit2"
},
"issuetype": {
"id": "10108"
}
Other things I would check:
The Reporter field is added to the Create Issue screen
The credentials these calls are run with, have Modify reporter
project permission (You will run into 405 error if they do not have
this permission)

Enhance Air ticket showing incorrect result in codeigniter

I am trying to post this JSON in post with URL
$url = 'https://api.havail.sabre.com/v1.2.0/air/ticket';
$headers2 = array(
'Authorization: bearer '.$access_token,
'protocol: HTTP 1.1 ',
'Accept: */*',
"Content-Type: application/json"
);
$postData ='{
"AirTicketRQ": {
"DesignatePrinter": {
"Profile": {
"Number": 1
}
},
"Itinerary": {
"ID": "VWKJJT"
},
"Ticketing": [{
"FOP_Qualifiers": {
"BSP_Ticketing": {
"MultipleFOP": {
"Fare": {
"Amount": "100.00"
},
"FOP_One": {
"CC_Info": {
"Suppress": true,
"PaymentCard": {
"Code": "VI",
"ExpireDate": "2016-11",
"ExtendedPayment": 12,
"Number": ***********1003
}
}
},
"FOP_Two": {
"Type": "CK"
}
}
}
},
"PricingQualifiers": {
"PriceQuote": [{
"Record": [{
"Number": 1,
"Reissue": true
}]
}]
}
},
{
"FOP_Qualifiers": {
"BSP_Ticketing": {
"MultipleFOP": {
"Fare": {
"Amount": "100.00"
},
"FOP_One": {
"CC_Info": {
"Suppress": true,
"PaymentCard": {
"Code": "VI",
"ExpireDate": "2016-11",
"ExtendedPayment": 12,
"Number": ************1003
}
}
},
"FOP_Two": {
"Type": "CA"
}
}
}
},
"PricingQualifiers": {
"PriceQuote": [{
"Record": [{
"Number": 2,
"Reissue": true
}]
}]
}
}],
"PostProcessing": {
"EndTransaction": {
"Source": {
"ReceivedFrom": "SPTEST"
}
}
}
}
}';
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_HTTPHEADER,$headers2);
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $postData);
//curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//var_dump( $results = json_decode(curl_exec($ch2)));
$results["get_data"] = json_decode(curl_exec($ch2),true);
I tried to use GET method for some other API and it was working. But for Enhanced Air ticket I am getting wrong data in response.
First Part:
Second Part:
What is the printer error and all. I used some get APIs that worked but why this API showing those problems? What is the actual problem? Is my JSON is correct that I am sending in request?
The problem is on this part of the code:
"DesignatePrinter": {
"Profile": {
"Number": 1
}
},
If this used to work, the you probably done it in a different environment or PCC.

Implementation of Bing Spell Check API v7 returns 0 flaggedTokens

I'm trying to implement the Bing Spell Check API v7. This is my current function:
# spell check
function bing_spell_check($q, $lang) {
$param = array();
$param['appName'] = PRO_NAME;
$param['text'] = $q;
$param['setLang'] = $lang;
$url = 'https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck?'.http_build_query($param);
$process = curl_init();
curl_setopt($process, CURLOPT_URL, $url);
curl_setopt($process, CURLOPT_TIMEOUT, 10);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_HTTPHEADER,
array(
'Accept: application/ld+json',
'Ocp-Apim-Subscription-Key: '.PRO_BING_KEY_SPELL
)
);
$response = curl_exec($process);
return $response;
}
The problem is that this example:
print_r(bing_spell_check('en', 'whts yur name?'));
Returns:
{
"#context":{
"#vocab":"http:\/\/bingapis.com\/v7\/schemas\/",
"s":"http:\/\/schema.org\/",
"#base":"https:\/\/api.cognitive.microsoft.com\/api\/v7\/"
},
"#type":"SpellCheck",
"flaggedTokens":[
]
}
Which means it has not found any errors. I ran the very same test in Bing's test tool, and I received this structure instead:
{
"_type": "SpellCheck",
"FlaggedTokens": [
{
"Offset": 0,
"Token": "whts",
"Type": "UnknownToken",
"Suggestions": [
{
"suggestion": "what's",
"Score": 0.909352914464075
},
{
"suggestion": "whats",
"Score": 0.810588859407343
},
{
"suggestion": "what is",
"Score": 0.680176771139565
}
]
},
{
"Offset": 5,
"Token": "yur",
"Type": "UnknownToken",
"Suggestions": [
{
"suggestion": "your",
"Score": 0.909352914464075
}
]
}
]
}
Am I missing something. Any ideas? Thanks!
It is quite simple, you just mixed up your function parameters. You have
function bing_spell_check($q, $lang) {}
and
bing_spell_check('en', 'whts yur name?');
That is where the problem starts, $q is now en and $lang is now whts yur name?. Thus when you create your $param array you have the language and the text mixed up (which will cause the Spell Check API to silently fail). You can simply fix this by changing the parameter order:
function bing_spell_check($lang, $q) {}
Now your code
<?php
define(PRO_BING_KEY_SPELL, 'XXX');
define(PRO_NAME, 'XXX');
function bing_spell_check($lang, $q) {
$param = array();
$param['appName'] = PRO_NAME;
$param['text'] = $q;
$param['setLang'] = $lang;
$url = 'https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?'.http_build_query($param);
$process = curl_init();
curl_setopt($process, CURLOPT_URL, $url);
curl_setopt($process, CURLOPT_TIMEOUT, 10);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_HTTPHEADER,
array(
'Accept: application/ld+json',
'Ocp-Apim-Subscription-Key: '.PRO_BING_KEY_SPELL
)
);
$response = curl_exec($process);
return $response;
}
print_r(bing_spell_check('en', 'whts yur name?'));
?>
results in
{
"#context": {
"#vocab": "http:\/\/bingapis.com\/v7\/schemas\/",
"s": "http:\/\/schema.org\/",
"#base": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/"
},
"#type": "SpellCheck",
"flaggedTokens": [{
"offset": 0,
"token": "whts",
"type": "UnknownToken",
"suggestions": [{
"suggestion": "what's",
"score": 0.909352914464075
}, {
"suggestion": "whats",
"score": 0.810588859407343
}, {
"suggestion": "what is",
"score": 0.680176771139565
}]
}, {
"offset": 5,
"token": "yur",
"type": "UnknownToken",
"suggestions": [{
"suggestion": "your",
"score": 0.909352914464075
}]
}]
}

PHP cURL request failing to balanced payments

I'm trying to submit a cURL request to Balanced Payment's test marketplace to simulate charging a credit card but I keep on getting an "unauthorized" status with "authentication-required" as the category_code. I've been able to successfully submit other requests using their documentation, so I'm not quite sure what I'm doing wrong.
I'm following these steps to charge a credit card in the test marketplace.
I'm only at step 1 which is to create an account to associate the card token with which is referred to as the URI.
//all of these parameters are supplied in the documentation
$url = 'https://api.balancedpayments.com/v1/marketplaces/TEST-MP3k9AuX9cW549vxxxxxxxxxxxxxxxx/accounts';
$post_arr[] = 'username=6d2896e8e9a911e2b3cd02xxxxxxxxxxxxxx:';
$post_arr[] = 'card_uri=/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/cards/CC3DSm7qtThxh6DRe6lnYNVC';
$post = implode('&',$post_arr);
//make the request
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3); // 3 seconds to connect
curl_setopt ($ch, CURLOPT_TIMEOUT, 10); // 10 seconds to complete
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
This is the output
{ "status": "Unauthorized", "category_code": "authentication-required", "category_type": "permission", "_uris": {}, "description": "Not permitted to perform show on cards. Your request id is OHMefab3f08ea6711e2950a026ba7xxxxxxxxxxxxx.", "request_id": "OHMefab3f08ea6711e2950a026baxxxxxxxxxxxx", "status_code": 401 }
However, I when I make the request with the following parameters to tokenize a card, it works.
$url = 'https://api.balancedpayments.com/v1/marketplaces/TEST-MP3k9AuX9cW549vxxxxxxxxxxxxx/cards';
$post_arr[] = '6d2896e8e9a911e2b3cd026bxxxxxxxxxxxxxx:';
$post_arr[] = 'expiration_month=12';
$post_arr[] = 'security_code=123';
$post_arr[] = 'card_number=5105105105105100';
$post_arr[] = 'expiration_year=2020';
$post_arr[] = 'category_type=request';
$post = implode('&',$post_arr);
Here is the output
{ "security_code_check": "true", "_type": "card", "hash": "b7250dd4b4827a88d5e4132b67f02916bce6f8e83d4ca0d779d1351b360ff6af", "brand": "MasterCard", "expiration_month": 12, "_uris": {}, "meta": {}, "last_four": "5100", "id": "CCSBORr685LWzmR62FSKZaw", "customer": null, "account": null, "postal_code_check": "true", "name": "None", "expiration_year": 2020, "created_at": "2013-07-11T21:01:53.245948Z", "uri": "/v1/marketplaces/TEST-MP3k9AuX9cW549vd1Ao5M0OW/cards/CCSBORr685LWzmR62FSKZaw", "card_type": "mastercard", "is_valid": true, "is_verified": true }
edit - I could still be doing something incorrectly, but I think there may be a mistake in the documentation
I removed the 'card_uri' parameter from my request and it succeeded. Upon checking out the returned JSON I noticed that it's 'cards_uri' not 'card_uri'. Maybe I'm still not doing something correctly, but when I changed my initial request to 'cards_uri' it then succeeded.
Here is the output now
{ "_type": "account", "_uris": { "holds_uri": { "_type": "page", "key": "holds" }, "bank_accounts_uri": { "_type": "page", "key": "bank_accounts" }, "refunds_uri": { "_type": "page", "key": "refunds" }, "customer_uri": { "_type": "customer", "key": "customer" }, "debits_uri": { "_type": "page", "key": "debits" }, "transactions_uri": { "_type": "page", "key": "transactions" }, "credits_uri": { "_type": "page", "key": "credits" }, "cards_uri": { "_type": "page", "key": "cards" } }, "bank_accounts_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/bank_accounts", "meta": {}, "transactions_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/transactions", "email_address": null, "id": "AC2A9aCbuzUWalMNX7JXbhrk", "credits_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/credits", "cards_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/cards", "holds_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/holds", "name": null, "roles": [], "created_at": "2013-07-11T21:24:55.567233Z", "uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk", "refunds_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/refunds", "customer_uri": "/v1/customers/AC2A9aCbuzUWalMNX7JXbhrk", "debits_uri": "/v1/marketplaces/TEST-MP24GBSQWq3M2nzdEtvvnHOf/accounts/AC2A9aCbuzUWalMNX7JXbhrk/debits" }
You are not authenticating. Try adding
curl_setopt('6d2896e8e9a911e2b3cd026ba7f8ec28:', CURLOPT_USERPWD)

Categories