How to generate a pass from 2 different json_encodes - php

I use this method to generate a pass: public function setJSON($JSON) {
if(json_decode($JSON) !== false) {
$this->JSON = $JSON;
return true;
}
$this->sError = 'This is not a JSON string.';
return false;
}
I call this method to generate pass by: $pass->setJSON('{
"passTypeIdentifier": "'.$passTypeID.'",
"formatVersion": 1,
..............
"barcode": {
"altText" : "'.$alt.'",
"format" : "PKBarcodeFormatPDF417",
"message": "Member-Card",
"messageEncoding": "iso-8859-1",
"changeMessage" : "This pass now has altText %# !"
},
"locations" : [
{
"longitude" : 104.89529371261597,
"latitude" : 11.576150037278605,
"relevantText": "CamMob (dis. 1%)"
},
.....................
]
}');
But now I don't write those locations statically and I get those data from database by : $query5 = mysql_query("select * from company");
while ($row5 = mysql_fetch_array($query5)){
$companyName = $row5['relevantTextName'];
$discount = $row5['relevantTextDiscount'];
$long = $row5['longitute'];
$lat = $row5['latitute'];
$link = $row5['link'];
$location['locations'][] = array("longitute" => $long, "latitute" => $lat, "relevantText" => $companyName." (" . $discount. "%)" );
}
$jsonString = json_encode($location) ;
error_log("Locations: ".$jsonString,0);
$pass->setJSON($jsonString);
$pass->setJSON('{
"passTypeIdentifier": "'.$passTypeID.'",
"barcode": {
"altText" : "'.$alt.'",
"format" : "PKBarcodeFormatPDF417",
"message": "Member-Card",
"messageEncoding": "iso-8859-1",
"changeMessage" : "This pass now has altText %# !"
}
}');</pre>
There is no error when I test for updating pass, but I cannot see the pass on lock screen like before. Thus it means that I not yet include those locations to the pass. What should I change ? Is it the problem of method setJSON ? If I do like this, how can I combine these 2 jsons to become together ?

Rather than constructing the JSON directly, construct an array first, then transform it into JSON.
$pass_data = array("passTypeIdentifier" => $passTypeID,
"formatVersion" => 1,
"barcode" => array (
"altText" => $alt,
"format" => "PKBarcodeFormatPDF417",
"message" => "Member-Card",
"messageEncoding" => "utf-8", // use UTF8 in case you want to encode Khmer or other non ASCII
"changeMessage" => "This pass now has altText %# !"
),
"locations" => $locationsArray,
"organizationName" => "Digi club card",
"description" => "Membership card",
"logoText" => "Digiclub",
"foregroundColor" => "rgb(0,0,0)",
"backgroundColor" => "rgb(211,211,211)",
"generic" => array (
"headerFields" => array(
array ( "key" => "Status",
"label" => " ",
"value" => "Membership card"
),
),
"primaryFields" => array(
array ( "key" => "Name",
"value" => $memberName,
),
),
"secondaryFields" => array(
// Secondary Field data
),
"auxiliaryFields" => array(
// Auxiliary Field data
),
"backFields" => array(
// Backfiels Field data
),
)
);
Then transform the array into JSON:
$pass->setJSON(json_encode($pass_data));

Related

How to get data from JSON file by parsing array name from the url

I am trying to print data in json format from my 'data.json' file. With my php file (alldata.php), I could get all data (arrays) pretty printed. But where I want you to help me is how to get a specific array name and it objects/content.
My alldata.php looks like this:
{
"players": [
{
"name": "Marcos Alonso",
"position": "Left-Back",
"nationality": "Spain",
"marketValue": "9,000,000 €",
"created": "2017-04-15 10:04:58"
}],
"articles": [
{
"author": "Stephen Walter",
"title": "Disruptive stag party revellers thrown off plane at Manchester Airport",
"url": "http://www.telegraph.co.uk/news/2017/04/15/disruptive-stag-party-revellers-thrown-plane-manchester-airport/",
"publishedAt": "2017-04-15T09:25:10Z"
}],
land": [
{
"state": "Somewhr",
"found": "1889",
"area": "6,812",
"empl": "1,325",
"ppl": "16,842"
}]
}
In php, how can I get an array e.g "players" with the content by using url such as 'alldata.php?search=players'
Here is a code sample....
//get content of the JSON API using file_get_contents()
$url = ('myJson.json');
$jsondata = file_get_contents($url);
//convert json object to php associative array
$data = json_decode($jsondata, true);
what do I do here to query the data.json file for a specific array?????
header('Content-Type: application/json; charset=UTF-8');
$json_string = json_encode($????????????, JSON_PRETTY_PRINT);
print $json_string;
Thanks
If I properly understood what you mean, and if your array has always the same tree, this wilp help you access the data :
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$array = array(
0 => array(
"players" => array(
"name" => "Marcos Alonso",
"position" => "Left-Back",
"nationality" => "Spain",
"marketValue" => "9,000,000 €",
"created" => "2017-04-15 10:04:58"
),
"articles" => array(
"author" => "Stephen Walter",
"title" => "Disruptive stag party revellers thrown off plane at Manchester Airport",
"url" => "http://www.telegraph.co.uk/news/2017/04/15/",
"publishedAt" => "2017-04-15T09:25:10Z"
),
"land" => array(
"state" => "Somewhr",
"found" => "1889",
"area" => "6,812",
"empl" => "1,325",
"ppl" => "16,842"
)
),
1 => array(
"players" => array(
"name" => "Sebastian Vettel",
"position" => "Driver",
"nationality" => "Germany",
"marketValue" => "9,000,000 €",
"created" => "2013-03-15 11:04:52"
),
"articles" => array(
"author" => "Stephen Walter",
"title" => "Disruptive stag party revellers thrown off plane at Manchester Airport",
"url" => "http://www.telegraph.co.uk/news/2017/04/15/",
"publishedAt" => "2017-04-15T09:25:10Z"
),
"land" => array(
"state" => "Somewhr",
"found" => "1889",
"area" => "6,812",
"empl" => "1,325",
"ppl" => "16,842"
)
)
);
/* end of array */
$data1 = json_encode($array); /* just checking - not needed after that */
$data = json_decode($data1, true); /* just checking - not needed after that */
$needle = "articles"; /* should be $needle = $_GET['search']; and checked before use */
//print_r($data); /* just checking */
foreach($data as $value){ /* we access 1st level */
echo '** Needle is: '.$needle.' **<br/>';
foreach($value[$needle] as $sub_key => $sub_data){ /* we access 2nd level */
echo $sub_key.'-->'.$sub_data.'<br/>'; }
}
?>
Once you access the data, you can easily do what you want with it...

change "0" to names in Json file

I am using PHP array and outputing to JSON string. I want to convert "0" in the json format to lets say calling them "items". Since while importing this to oracle db it says expecting names instead of zero. When I change the index ( 0, 1, 2 ) to be called just items, it works normally.
Here is the PHP array, and I am outputting it as json.
$data = array(
'INDIVIDUAL_SALUTATION' => $salution,
'INDIVIDUAL_FIRST_NAME' => $firstname,
'INDIVIDUAL_LAST_NAME' => $lastname,
'GENDER' => $gender,
'DATE_OF_BIRTH' => $result['Birthday'],
'EMAIL_ID' => $result['Email'],
'MOBILE_NUMBER' =>$result['Phone'],
'MOBILE_COUNTRY_CODE' => substr($result['Phone'], 1, 3),
'OCCUPATION' => null,
'OCCUPATION_STATUS' => null,
'ADDRESS_LINE1' => $result['Address_street'],
'TOWN' => $result['Address_city'],
'POSTAL_CODE' => $result['Address_zip'],
'COUNTRY' => $result['country_name'],
'CUSTOMER_NUMBER' => $result['Owner'],
'POLICY_START_DATE' => $result['paid_thru_date'],
'POLICY_END_DATE' => $result['duedate'],
'LOAN_AGREEMENT_NUMBER' => $result['ORIG_ID'],
'REPAYABLE_AMOUNT' => $result['repayable_amount'],
'FINANCE_TERM_MONTHS' => $result['finance_term_months'],
'MONTHLY_INSTALLMENT' => $result['monthly_installment'],
'AMOUNT_INSURED' => $result['amount_insured'],
'CURRENCY_ID' => $result['Abbreviation']
);
$jsonArray[] = $data;
}
$mainInfo = array(
'SRC_NAME' => 'AEX',
"RUN_NUMBER" => 1,
"RUN_DATE" => date("Ymd"),
"RUN_NO_OF_RECORDS" => $arrayCount,
"YTD_NO_OF_RECORDS" => $arrayCount
);
$finalArray = array_merge($jsonArray , $mainInfo);
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($finalArray));
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($mainInfo));
Here is the output
{
"0":{
"INDIVIDUAL_SALUTATION":"MR",
"INDIVIDUAL_FIRST_NAME":"borrower",
"INDIVIDUAL_LAST_NAME":"three",
"GENDER":"M",
"DATE_OF_BIRTH":"1993-09-17",
"EMAIL_ID":"borrowerthree#aurorax.co",
"MOBILE_NUMBER":"+3581466144569",
"MOBILE_COUNTRY_CODE":"358",
"OCCUPATION":null,
"OCCUPATION_STATUS":null,
"ADDRESS_LINE1":"Vaskivuorentie 22B",
"TOWN":"Vantaa",
"POSTAL_CODE":"01600",
"COUNTRY":"Finland",
"CUSTOMER_NUMBER":"772",
"POLICY_START_DATE":"2017-01-02",
"POLICY_END_DATE":"2017-07-01",
"LOAN_AGREEMENT_NUMBER":"7",
"REPAYABLE_AMOUNT":"50.42",
"FINANCE_TERM_MONTHS":"6",
"MONTHLY_INSTALLMENT":"8.40",
"AMOUNT_INSURED":"50.42",
"CURRENCY_ID":"EUR"
},
"1":{
"INDIVIDUAL_SALUTATION":"MR",
"INDIVIDUAL_FIRST_NAME":"borrower",
"INDIVIDUAL_LAST_NAME":"three",
"GENDER":"M",
"DATE_OF_BIRTH":"1993-09-17",
"EMAIL_ID":"borrowerthree#aurorax.co",
"MOBILE_NUMBER":"+3581466144569",
"MOBILE_COUNTRY_CODE":"358",
"OCCUPATION":null,
"OCCUPATION_STATUS":null,
"ADDRESS_LINE1":"Vaskivuorentie 22B",
"TOWN":"Vantaa",
"POSTAL_CODE":"01600",
"COUNTRY":"Finland",
"CUSTOMER_NUMBER":"772",
"POLICY_START_DATE":"2017-01-02",
"POLICY_END_DATE":"2017-07-01",
"LOAN_AGREEMENT_NUMBER":"9",
"REPAYABLE_AMOUNT":"40.35",
"FINANCE_TERM_MONTHS":"6",
"MONTHLY_INSTALLMENT":"6.73",
"AMOUNT_INSURED":"40.35",
"CURRENCY_ID":"EUR"
},
"2":{
"INDIVIDUAL_SALUTATION":"MR",
"INDIVIDUAL_FIRST_NAME":"borrower",
"INDIVIDUAL_LAST_NAME":"two",
"GENDER":"M",
"DATE_OF_BIRTH":"1993-09-17",
"EMAIL_ID":"borrowertwo#aurorax.co",
"MOBILE_NUMBER":"+358466144569123",
"MOBILE_COUNTRY_CODE":"358",
"OCCUPATION":null,
"OCCUPATION_STATUS":null,
"ADDRESS_LINE1":"Vaskivuorentie 22B",
"TOWN":"Vantaa",
"POSTAL_CODE":"01600",
"COUNTRY":"Finland",
"CUSTOMER_NUMBER":"770",
"POLICY_START_DATE":"2017-01-02",
"POLICY_END_DATE":"2017-07-01",
"LOAN_AGREEMENT_NUMBER":"11",
"REPAYABLE_AMOUNT":"99.84",
"FINANCE_TERM_MONTHS":"6",
"MONTHLY_INSTALLMENT":"16.64",
"AMOUNT_INSURED":"99.84",
"CURRENCY_ID":"EUR"
},
"RUN_NUMBER":1,
"RUN_DATE":"20170109"
}
What I am trying to do is make it look like instead of 0 have 'items' : {} and then move on, so I can import it to oracle.
How can I achieve this with php?
Don't use array_merge, put the array in an element of $mainInfo:
$mainInfo['items'] = $jsonArray;
$this->output->set_output(json_encode($mainInfo));
Create index variable and make it to zero
$index = 0
And change this $jsonArray[] = $data; to this $jsonArray['item' .$index++] = $data;
Hope that helps

how to set the php json encode multi dimensional array

Good morning,
I have a very specific example that I am trying to implement. The goal is to get the following JSON end result.
{
merchantId :"123456",
tenderType :"Card",
amount :"0.02",
account :
{
number : "4111",
expiryMonth : "02",
expiryyear : "2016",
cvv : "019",
avsZip : "30014",
avsStreet: "2001 Main"
}
}
I am familiar with json_encode and I have can get this done for the 1st 3 parameters with the following code:
json_encode(
array(
"merchantId" => "123456",
"tenderType" => "Card",
"amount" => "0.02"
)
}
but the 4th parameter (account) is getting me stuck. Can anybody explain to me how to incorporate the 4th parameter that is itself an array.
George
It should just be a nested associative array:
json_encode(
array(
"merchantId" => "123456",
"tenderType" => "Card",
"amount" => "0.02",
"account" => array(
"number" => "4111",
"expiryMonth" => "02",
"expiryyear" => "2016",
"cvv " => "019",
"avsZip" => "30014",
"avsStreet" => "2001 Main"
)
)
)

how to aggregate mongodb collection data in laravel

i have collection like this
{
"wl_total" : 380,
"player_id" : 1241,
"username" : "Robin",
"hand_id" : 292656,
"time" : 1429871584
}
{
"wl_total" : -400,
"player_id" : 1243,
"username" : "a",
"hand_id" : 292656,
"time" : 1429871584
}
as both collection have same hand_id i want to aggregate both these collection on the basis of hand_id
i want result as combine of
data=array(
'hand_id'=>292656,
'wl_total'=>
{
0=>380,
1=>-400
},
'username'=>
{
0=>"Robin",
1=>"a"
},
"time"=>1429871584
)
You basically want a $group by the "hand_id" common to all players, and then $push to different arrays in the document and then also do something with "time", I took $max. Nees to be an accumulator of some sort at any rate.
Also not sure what your underlying collection name is, but you can call this in laravel with a construct like this:
$result = DB::collection('collection_name')->raw(function($collection)
{
return $collection->aggregate(array(
array(
'$group' => array(
'_id' => '$hand_id',
'wl_total' => array(
'$push' => '$wl_total'
),
'username' => array(
'$push' => '$username'
),
'time' => array(
'$max' => '$time'
)
)
)
));
});
Which returns output ( shown in json ) like this:
{
"_id" : 292656,
"wl_total" : [
380,
-400
],
"username" : [
"Robin",
"a"
],
"time" : 1429871584
}
Personally I would have gone for a single array with all the infomation in it for the grouped "hand", but I supose you have your reasons why you want it this way.

Adding Docusign Template array into REST Header

So I'm back again. My problem is this:
I have an array of Docusign Templates from checkboxes in a Codeigniter view:
<?php
echo form_open('create_envelope');
foreach ($response["envelopeTemplates"] as $envelopeTemplate) { ?>
<li><?php echo form_checkbox('templatearray[]', $envelopeTemplate["templateId"], FALSE), $envelopeTemplate["name"]; ?></li>
<?php } ?>
What I'm trying to do is add the templates to our REST Header request:
$data = array(
"accountId" => $accountId,
"emailSubject" => "Hello World!",
"emailBlurb" => "This comes from PHP",
"templateId" => "ID from template array here",
"templateRoles" => array(
array(
"tabs" => array(
"textTabs" => array (
array (
"tabLabel" => "lic_num",
"value" => "$license_number"
),
array (
"tabLabel" => "ubi_num",
"value" => "$ubi_number"
),
array (
"tabLabel" => "tra_nam",
"value" => "$trade_name"
)
)
),
"email" => "$applicant_email",
"name" => "$applicant_name",
"roleName" => "Applicant"
)
),
"status" => "sent"
);
Is this possible?
EDIT: So I got it to work using loops to get my data in the request, but I'm running into an interesting problem. If I put one or two templates in the envelope, it sends fine. If I put more than two in, it duplicates the templates. Here is my code for the complicated loops:
$compTempArray = array();
$applicant_name = $this->input->post("applicant_name");
$applicant_email = $this->input->post("applicant_email");
$license_number = $this->input->post("license_number");
$ubi_number = $this->input->post("ubi_number");
$trade_name = $this->input->post("trade_name");
foreach($hello as $key => $value) {
if(sizeof($hello) > 1) {
for($i = 1; $i < sizeof($hello); $i++) {
$compTempArray[] = array("serverTemplates" => array(
array(
"sequence" => $i,
"templateId" => $value
)
),
"inlineTemplates" => array(
array(
"sequence" => $i,
"recipients" => array(
"signers" => array(
array(
"tabs" => array(
"textTabs" => array (
array ("tabLabel" => "lic_num", "value" => $license_number),
array ("tabLabel" => "ubi_num", "value" => $ubi_number),
array ("tabLabel" => "tra_nam", "value" => $trade_name)
)
),
"email" => "*********#*****.com",
"name" => $applicant_name,
"recipientId" => "1",
"roleName" => "Applicant"
),
)
)
)
));
}
$data = array("accountId" => $accountId,
"emailSubject" => "Hello World!",
"emailBlurb" => "This comes from PHP",
"compositeTemplates" => $compTempArray,
"status" => "sent");
} else {
$data = array("accountId" => $accountId,
"emailSubject" => "Hello World!",
"emailBlurb" => "This comes from PHP",
"templateId" => "$value",
"templateRoles" => array(
array(
"tabs" => array(
"textTabs" => array (
array ("tabLabel" => "lic_num", "value" => $license_number),
array ("tabLabel" => "ubi_num", "value" => $ubi_number),
array ("tabLabel" => "tra_nam", "value" => $trade_name)
)
),
"email" => "*********#*****.com",
"name" => $applicant_name,
"roleName" => "Applicant"
)
),
"status" => "sent");
}
}
Any idea why it would do this?
NEW EDIT: Update on this weirdness: one to two - one copy of each template, three - it doubles the amount of each template, four - it triples the amount, five - it quadruples the amount.
NEWEST EDIT: So as it turns out, it was the for loop that I was using to try and increment the sequence. I got rid of the loop and hardcoded the sequence to 1. That fixed it.
To apply multiple templates to a single envelope you'll need to use the compositeTemplates structure.
compositeTemplates can get complex very quickly but they do allow for great flexibility and functionality for your envelopes. The API documentation is the best place to read about compositeTemplates but as previously mentioned the April 2012 Templates Webinar is also a good resource. The third example provides a basic use of compositeTemplates in that it shows you how to combine two server templates into one single envelope. You can use that as a base for your JSON.
To apply 2 server templates to a single envelope it uses the following JSON:
{
"emailSubject": "DocuSign Templates Webinar - Example 3",
"emailBlurb": "Example #3 - Composite Templates",
"status": "sent",
"compositeTemplates": [
{
"serverTemplates": [
{
"sequence": "1",
"templateId": "55A80182-2E9F-435D-9B16-FD1E1C0F9D74"
}
],
"inlineTemplates": [
{
"sequence": "1",
"recipients": {
"signers": [
{
"email": "firstrecipient#gmail.com",
"name": "John Doe",
"recipientId": "1",
"roleName": "RoleOne"
}
]
}
}
]
},
{
"serverTemplates": [
{
"sequence": "2",
"templateId": "44D9E888-3D86-4186-8EE9-7071BC87A0DA"
}
],
"inlineTemplates": [
{
"sequence": "2",
"recipients": {
"signers": [
{
"email": "secondrecipient#gmail.com",
"name": "Jane Doe",
"recipientId": "1",
"roleName": "RoleOne"
}
]
}
}
]
}
]
}
Note that the sequence value for each template determines the order of template application to the envelope. So in other words, the sequence value determines the document order, but since the templates might have matching/conflicting info (in terms of template roles for instance) the sequence value might also affect the end result of the envelope.

Categories