dpbl91234 2016-12-31 01:01
浏览 73

MyEmma API PHP数组和For或While循环

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?

  • 写回答

1条回答 默认 最新

  • dongtu1357 2016-12-31 01:16
    关注

    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!
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图