Answers.Body: <p>Fixed using the following code. Also includes fetching an API via Guzzle and parsing it into Craft CMS. Thought I would post here so that if anyone else is having trouble they can decipher bits from this. </p>
  
  <pre><code>                // Set a base URI
                  $baseUri = 'http://domain.com';
                  // Build the Guzzle Client and use the Base URI
                  $client = Craft::createGuzzleClient([
                    'base_uri' =&gt; $baseUri,
                  ]);
                  // Send a Request!
                  $response = $client-&gt;request('GET', '/mytestendpoint/');
                  // Parse the Response
                  $json = $response-&gt;getBody();
                  // Decode it
                  $data = json_decode($json, true);
  
                  // Set some values from it!
                  $user-&gt;setFieldValue('userTotalFollowers', $data['followers'] );
                  $user-&gt;setFieldValue('userEngagementRating', $data['details']['total_likes'] );
                  $user-&gt;setFieldValue('verified', $data['is_verified'] );
  
                  $number = 0;
                  $matrixData = array();
  
                  foreach($data['media'] as $element) {
                    $number++;
                    $matrixData["new$number"] = array(
                      'type' =&gt; 'media',
                      'enabled' =&gt; true,
                      'fields' =&gt; array(
                        'mediaDescription' =&gt; $element['likes'],
                        'mediaLikes' =&gt; count($element),
                      )
                    );
                  }
                  $user-&gt;setFieldValue('recentPosts', $matrixData);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-29T00:03:43.707Z
Answers.LastActivityDate: 2019-05-29T00:03:43.707Z
Answers.UserDisplayName: JMKelley
Body: <p>I'm having an issue creating a matrix block from a module. I'm currently doing the following:</p>
  
  <ul>
  <li>Calling an API using Guzzle</li>
  <li>Getting that request and iterating over a <code>media</code> block.</li>
  <li>Trying to create a new Matrix Block with each iteration.</li>
  <li>Adding data from the request into each Matrix Block.</li>
  </ul>
  
  <p>** EDIT **</p>
  
  <p>I got this working using the following code:</p>
  
  <pre><code>                $data = json_decode($json, true);
                  $number = 1;
  
                  foreach($data['media'] as $element) {
                    $matrixData = array();
                    $matrixData["new$number"] = array(
                      'type' =&gt; 'media',
                      'enabled' =&gt; true,
                      'fields' =&gt; array(
                        'mediaDescription' =&gt; $element['likes'],
                      )
                    );
                    $user-&gt;setFieldValue('recentPosts', $matrixData);
                    $number++;
                  }
  </code></pre>
  
  <p>Example JSON</p>
  
  <pre><code>"media": [
          {
              "media_id": "2053313765945828720",
              "instagram_user_id": "1",
              "shortcode": "Bx-1f2_nuVw",
              "created_date": "1558994096",
              "caption": "",
              "comments": "7077",
              "likes": "1131719",
              "media_url": "https://www.instagram.com/p/Bx-1f2_nuVw",
              "media_image_url": "https://scontent-lhr3-1.cdninstagram.com/vp/8e6c054cec7d3c3657bc73d49dae0fd9/5D7B651B/t51.2885-15/e35/c0.134.1080.1080/s150x150/61993210_834852143539503_2474404095511077560_n.jpg?_nc_ht=scontent-lhr3-1.cdninstagram.com",
              "type": "IMAGE",
              "mentions": [],
              "hashtags": [],
              "date": "2019-05-28 02:44:09",
              "last_check_date": "2019-05-28 20:25:24"
          },
  ...
  </code></pre>
  
  <p>Unfortunately, now my forloop isn't saving new matrix blocks.</p>
  
Tags: <plugins><php><modules><guzzle>
Title: Create Matrix Block via Module
ThreadText: # Create Matrix Block via Module
  
  (Asked by: username_0 on 2019-05-28)
  
  I'm having an issue creating a matrix block from a module. I'm currently doing the following:
  
  * Calling an API using Guzzle
  * Getting that request and iterating over a `media` block.
  * Trying to create a new Matrix Block with each iteration.
  * Adding data from the request into each Matrix Block.
  
  \*\* EDIT \*\*
  
  I got this working using the following code:
  
  ```
                  $data = json_decode($json, true);
                  $number = 1;
  
                  foreach($data['media'] as $element) {
                    $matrixData = array();
                    $matrixData["new$number"] = array(
                      'type' => 'media',
                      'enabled' => true,
                      'fields' => array(
                        'mediaDescription' => $element['likes'],
                      )
                    );
                    $user->setFieldValue('recentPosts', $matrixData);
                    $number++;
                  }
  
  ```
  
  Example JSON
  
  ```
  "media": [
          {
              "media_id": "2053313765945828720",
              "instagram_user_id": "1",
              "shortcode": "Bx-1f2_nuVw",
              "created_date": "1558994096",
              "caption": "",
              "comments": "7077",
              "likes": "1131719",
              "media_url": "https://www.instagram.com/p/Bx-1f2_nuVw",
              "media_image_url": "https://scontent-lhr3-1.cdninstagram.com/vp/8e6c054cec7d3c3657bc73d49dae0fd9/5D7B651B/t51.2885-15/e35/c0.134.1080.1080/s150x150/61993210_834852143539503_2474404095511077560_n.jpg?_nc_ht=scontent-lhr3-1.cdninstagram.com",
              "type": "IMAGE",
              "mentions": [],
              "hashtags": [],
              "date": "2019-05-28 02:44:09",
              "last_check_date": "2019-05-28 20:25:24"
          },
  ...
  
  ```
  
  Unfortunately, now my forloop isn't saving new matrix blocks.
  
  **Tags:** plugins, php, modules, guzzle
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-05-29. Score: 2 | Accepted answer
  
  Fixed using the following code. Also includes fetching an API via Guzzle and parsing it into Craft CMS. Thought I would post here so that if anyone else is having trouble they can decipher bits from this.
  
  ```
                  // Set a base URI
                  $baseUri = 'http://domain.com';
                  // Build the Guzzle Client and use the Base URI
                  $client = Craft::createGuzzleClient([
                    'base_uri' => $baseUri,
                  ]);
                  // Send a Request!
                  $response = $client->request('GET', '/mytestendpoint/');
                  // Parse the Response
                  $json = $response->getBody();
                  // Decode it
                  $data = json_decode($json, true);
  
                  // Set some values from it!
                  $user->setFieldValue('userTotalFollowers', $data['followers'] );
                  $user->setFieldValue('userEngagementRating', $data['details']['total_likes'] );
                  $user->setFieldValue('verified', $data['is_verified'] );
  
                  $number = 0;
                  $matrixData = array();
  
                  foreach($data['media'] as $element) {
                    $number++;
                    $matrixData["new$number"] = array(
                      'type' => 'media',
                      'enabled' => true,
                      'fields' => array(
                        'mediaDescription' => $element['likes'],
                        'mediaLikes' => count($element),
                      )
                    );
                  }
                  $user->setFieldValue('recentPosts', $matrixData);
  
  ```
  
  
  ---
  
  