Mailchimp JSON using PHP foreach with multiple keys - php

Im using Mailchimp API and getting their user activity. From the array, I have to GET the campaign_id = 1ce6d076f4 first and then match the Campaign ID to all arrays that has action = click so that I can get value of url. Here's the JSON array of the mailchimp.
Array
(
[0] => stdClass Object
(
[action] => click
[timestamp] => 2019-11-12T03:08:40+00:00
[url] => https://nasis.sb:8890/article/burlington-vermont-market-overview
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[1] => stdClass Object
(
[action] => click
[timestamp] => 2019-11-12T02:54:07+00:00
[url] => https://nasis.sb:8890/property/walgreens-burlington-vermont?lid=*|HTML:LINKID|*
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[2] => stdClass Object
(
[action] => open
[timestamp] => 2019-11-12T02:33:55+00:00
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[3] => stdClass Object
(
[action] => sent
[timestamp] => 2019-11-12T02:33:40+00:00
[type] => regular
[campaign_id] => 1ce6d076f4
[title] => Sample Campaign v2
)
[4] => stdClass Object
(
[action] => open
[timestamp] => 2019-10-31T00:38:02+00:00
[campaign_id] => fbe8dfde89
[title] => Sample Campaign v1
)
[5] => stdClass Object
(
[action] => click
[timestamp] => 2019-10-31T00:15:44+00:00
[url] => https://nasis.sb:8890?lid=*|HTML:LINKID|*
[campaign_id] => fbe8dfde89
[title] => Sample Campaign v1
)
)
I am connecting the mailchimp api via wordpress plugin. And here's my code. However, nothing happen when trying to display the $x
$activityDetails = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.$subscriber_hash.'/activity', $args );
$body = json_decode( wp_remote_retrieve_body( $activityDetails ) );
// echo '<pre>'; print_r($body->activity); echo '</pre>';
foreach ( $body->activity as $act ) {
if ( $act->campaign_id == '1ce6d076f4' ) {
$x = $act->action;
}
}
echo '<p> Campaign ID: ' . $x . '</p>';
UPDATE
It looks like I got it to work...
foreach ( $body->activity as $act ) {
if ( $act->campaign_id == '1ce6d076f4' ) {
if ( $act->action == 'click' ) {
$x[] = $act->url;
}
}
}
echo '<pre>'; print_r($x); echo '</pre>';
// Output
Array
(
[0] => https://www.nasinvestmentsolutions.com/article/burlington-vermont-market-overview
[1] => https://nasis.sb:8890/property/walgreens-burlington-vermont?lid=*|HTML:LINKID|*
)

Related

Retrieve a value from JSON Object using PHP (Shiprocket API)

I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.
I have tried to use json_decode but it did not work and show null value:
$data = json_decode($json);
$sr_status = $data['shipment_status'];
Please suggest the code to retrieve below variables value.
shipment_status , awb_code , courier_company_id
Array
(
[0] => stdClass Object
(
[tracking_data] => stdClass Object
(
[track_status] => 1
[shipment_status] => 7
[shipment_track] => Array
(
[0] => stdClass Object
(
[id] => 180339484
[awb_code] => 11150911492
[courier_company_id] => 55
[shipment_id] => 1711169662
[order_id] => 233223781187
[pickup_date] => 2023-01-11 03:02:00
[delivered_date] => 2023-01-16 12:22:00
[weight] => 0.25
[packages] => 1
[current_status] => Delivered
[delivered_to] => Solapur
[destination] => Solapur
[consignee_name] => ABC
[origin] => Ludhiana
[courier_agent_details] =>
[edd] =>
)
)
[shipment_track_activities] => Array
(
[0] => stdClass Object
(
[date] => 2023-01-16 12:22:00
[status] => 000-T-DL
[activity] => SHIPMENT DELIVERED
[location] => SOLAPUR
[sr-status] => 7
[sr-status-label] => DELIVERED
)
[1] => stdClass Object
(
[date] => 2023-01-16 11:34:00
[status] => 002-S-UD
[activity] => SHIPMENT OUTSCAN
[location] => SOLAPUR
[sr-status] => 17
[sr-status-label] => OUT FOR DELIVERY
)
)
[track_url] => https://shiprocket.co//tracking/11150911492
[etd] => 2023-01-14 17:02:00
[qc_response] => stdClass Object
(
[qc_image] =>
[qc_failed_reason] =>
)
)
)
)
you can try this:
$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;
Or use $data = json_decode($json,true); which return an array where you can use
foreach($data as $val) {
$shipment_status = $val['tracking_data']['shipment_status'];
foreach ($val['shipment_track'] as $value) {
$awb_code = $value['awb_code'];
$courier_company_id = $value['courier_company_id'];
}
}

Request for php foreach stdclass example

Someone please can explain me how i can get the values from entry_list in a foreach loop in php?
Following is the return data structure. Please help.
stdClass Object
(
[result_count] => 1
[total_count] => 1
[next_offset] => 1
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697
[module_name] => Leads
[name_value_list] => stdClass Object
(
[assigned_user_name] => stdClass Object
(
[name] => assigned_user_name
[value] => Administrator
)
[modified_by_name] => stdClass Object
(
[name] => modified_by_name
[value] => Administrator
)
[created_by_name] => stdClass Object
(
[name] => created_by_name
[value] => Administrator
)
[id] => stdClass Object
(
[name] => id
[value] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697
)
Try this,
foreach($arr->entry_list as $row) {
foreach($row->name_value_list as $key => $val){
echo $key;// will echo assigned_user_name
echo $val->name; // assigned_user_name
echo $val->value; // Administrator
}
}
Sample values are given for first row... Similarly, all values will get printed.

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

How to get the user 'contactid' using google API using PHP

I am trying to fetch user contacts with all user details uaing the google API 3.0.
I am able to get the JSON response with the details of the user.
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
The result is
[entry] => Array
(
[0] => Array
(
[id] => Array
(
[$t] => http://www.google.com/m8/feeds/contacts/sfdhitdf1%40gmail.com/base/1
)
[gd$etag] => "RXc7fTVSLit7I2A9XRZaEkoLRAw."
[updated] => Array
(
[$t] => 2014-08-29T00:16:24.905Z
)
[app$edited] => Array
(
[xmlns$app] => http://www.w3.org/2007/app
[$t] => 2014-08-29T00:16:24.905Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => abc
)
[link] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/contacts/2008/rel#photo
[type] => image
[href] => https://www.google.com/m8/feeds/photos/media/sachdfwdfd%40gmail.com/1?v=3.0
[gd$etag] => "VWVIH3oyWit7I2B0UBRURzwNBWM8ODs8cSk."
)
[1] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/sachitdff%40gmail.com/full/1?v=3.0
)
[2] => Array
(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/sachidtfd%40gmail.com/full/1?v=3.0
)
)
[gd$name] => Array
(
[gd$fullName] => Array
(
[$t] => abc xyz
)
[gd$givenName] => Array
(
[$t] => abc
)
[gd$familyName] => Array
(
[$t] => xyz
)
)
[gd$email] => Array
(
[0] => Array
(
[address] => abi9#gmail.com
[primary] => true
[rel] => http://schemas.google.com/g/2005#other
)
)
[gContact$website] => Array
(
[0] => Array
(
[href] => http://www.google.com/profiles/104048264070958665151
[rel] => profile
)
)
[gContact$groupMembershipInfo] => Array
(
[0] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/sachitaware
)
)
But here I dont get the contact image of the user.The documentation says I need a contact id for getting the photo,but I dont get a contact id in the response above.How can I get the contact id of the user and subsequently his contact photo?
I have authorized the app using oauth 2.0 and apart from image I get most of the details of the contact.
EDIT: I tried this way from the documentation and it works,but it returns the binary image instead of the image URL and I have to send another request to get the image.
$url1 ='https://www.google.com/m8/feeds/photos/media/{useremail}/13444? v=3.0&oauth_token='.$accesstoken;
$xmlresponse1 = curl($url1);
To display the image:
<img src="data:image/*;base64,<?php echo base64_encode($xmlresponse1); ?> />
Can't I get the contact image URL something like facebook returns?
According the documentation, contactIid is returned in the contact entry URL returned by the API:
http://www.google.com/m8/feeds/contacts/<userEmail>/base/<contactId>
So, giving your sample:
http://www.google.com/m8/feeds/contacts/sfdhitdf1%40gmail.com/base/1
We have these values:
userEmail: sfdhitdf1#gmail.com
contactId: 1

How to parse information in PHP?

I have the following text below, what is the easiest way to parse through it?
if(typeof sa_inst.apiCB == 'function') sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);
I would like to only get what is after ("Txt":) and not including the <strong> or anything else. For example, form the text above I would like to get something like:
google, gmail, google maps, google images, etc
in an array. Help please?
The text is JSON. Use json_decode to parse it.
$str = '{"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}}';
$result = json_decode($str);
//Show all 'Txt' values
foreach($result->AS->Results[0]->Suggests as $suggests)
echo "Txt contains: ".htmlspecialchars($suggests->Txt). "<br/>";
//Show full contents of result
echo "<pre>";
print_r($result);
This outputs values of Txt first:
Txt contains: g<strong>mail</strong>
Txt contains: g<strong>oogle</strong> <strong>maps</strong>
Txt contains: g<strong>oogle</strong> <strong>earth</strong>
Txt contains: g<strong>ames</strong>
Txt contains: g<strong>oogle</strong> <strong>images</strong>
Txt contains: g<strong>amestop</strong>
Txt contains: g<strong>rainger</strong>
And then the complete JSON data too:
stdClass Object
(
[AS] => stdClass Object
(
[Query] => g
[FullResults] => 1
[Results] => Array
(
[0] => stdClass Object
(
[Type] => AS
[Suggests] => Array
(
[0] => stdClass Object
(
[Txt] => google
[Type] => AS
[Sk] =>
)
[1] => stdClass Object
(
[Txt] => gmail
[Type] => AS
[Sk] => AS1
)
[2] => stdClass Object
(
[Txt] => google maps
[Type] => AS
[Sk] => AS2
)
[3] => stdClass Object
(
[Txt] => google earth
[Type] => AS
[Sk] => AS3
)
[4] => stdClass Object
(
[Txt] => games
[Type] => AS
[Sk] => AS4
)
[5] => stdClass Object
(
[Txt] => google images
[Type] => AS
[Sk] => AS5
)
[6] => stdClass Object
(
[Txt] => gamestop
[Type] => AS
[Sk] => AS6
)
[7] => stdClass Object
(
[Txt] => grainger
[Type] => AS
[Sk] => AS7
)
)
)
)
)
)
This will work, but I removed ' from 'function'
<?php
echo $text = 'if(typeof sa_inst.apiCB == function sa_inst.apiCB({"AS":{"Query":"g","FullResults":1,"Results":[{"Type":"AS","Suggests":[{"Txt":"g<strong>oogle</strong>","Type":"AS","Sk":""},{"Txt":"g<strong>mail</strong>","Type":"AS","Sk":"AS1"},{"Txt":"g<strong>oogle</strong> <strong>maps</strong>","Type":"AS","Sk":"AS2"},{"Txt":"g<strong>oogle</strong> <strong>earth</strong>","Type":"AS","Sk":"AS3"},{"Txt":"g<strong>ames</strong>","Type":"AS","Sk":"AS4"},{"Txt":"g<strong>oogle</strong> <strong>images</strong>","Type":"AS","Sk":"AS5"},{"Txt":"g<strong>amestop</strong>","Type":"AS","Sk":"AS6"},{"Txt":"g<strong>rainger</strong>","Type":"AS","Sk":"AS7"}]}]}} /* pageview_candidate */);';
echo '<br />';
$text = strip_tags($text); // remove html tags
$a = explode('"Txt":"', $text);
$app = array();
foreach ($a as $c) {
$b = explode('"', $c);
$app = $b[0];
}
unset($app[0]);
var_dump($app);
?>

Categories