I am working with the MyEmma API. MyEmma is a cloud based email marketing software. Although if you have not heard of it or worked with it you still may be able to offer some assistance with my question as it deals primarily with a data array and extracting the values from a For or While PHP loop.
So I have the following code that will output the $head variable at the end using print_r($head) as you can see in the bottom of the code:
<?php
// Authentication Variables
$account_id = "1788878";
$public_api_key = "ccXXXXXXXXXXXXX3";
$private_api_key = "cXXXXXXXXXXXXX5";
// Set URL
$url = "https://api.e2ma.net/" . $account_id . "/members";
// Open connection
$ch = curl_init();
// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $public_api_key . ":" . $private_api_key);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// check for errors
if($http_code > 200) {
print "Error getting member:
";
print_r($head);
} else {
print "Member information:
";
print_r($head);
}
?>
The following output is an example generated by print_r($head):
[ { "status": "active", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158888, "last_modified_at": null, "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:09:31", "bounce_count": 0, "deleted_at": null, "email": "david@something.com" }, { "status": "error", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158889, "last_modified_at": "@D:2014-06-09T09:00:19", "member_status_id": "e", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:10:15", "bounce_count": 1, "deleted_at": null, "email": "test@something.com" } ]
In the above output example these are just two entities of hundreds the script actually outputs.
What I would like to accomplish is using a 'for' or 'while' loop in PHP or something similar in order to grab each "member_id" value and "email" value and that will be used in a SQL INSERT statement to create a database record for each set of member_id and email value.
What I need help with is the syntax for creating the loop to extract both of the values for each record I will create in a database table. Can someone show me how to correctly write the code in order to do this?
You are getting a proper json as return. So you can decode it and use it as array.
<?php
$head = '[ { "status": "active", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158888, "last_modified_at": null, "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:09:31", "bounce_count": 0, "deleted_at": null, "email": "david@something.com" }, { "status": "error", "confirmed_opt_in": null, "account_id": 1788878, "fields": { }, "member_id": 258158889, "last_modified_at": "@D:2014-06-09T09:00:19", "member_status_id": "e", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-01-30T11:10:15", "bounce_count": 1, "deleted_at": null, "email": "test@something.com" } ]';
$data = json_decode($head,true); // the second param will cast it as array, default is obejct. See the linked docs of json_decode!
foreach($data as $item) {
$id=$item['member_id'];
$email=$item['email'];
echo "ID: $id EMAIL: $email<br/>";
// put your code here to write into DB or whatever you wanna do!
}
?>
Alternative: use it as objects:
<?php
$data = json_decode($head); // defaults to object if second param isn't set.
foreach($data as $item) {
$id=$item->member_id;
$email=$item->email;
echo "ID: $id EMAIL: $email<br/>";
// put your code here to write into DB or whatever you wanna do!
}
?>