Answers.Body: <p>I assume that <code>$block-&gt;tweet-&gt;one()</code> returns an Asset element in your matrix block called <code>tweet</code>. I suggest that you use the <code>getContents()</code> method on Craft's Asset element type to retrieve the string content of the actual file. Your code block would look something like this:</p>
  <pre><code>case 'tweet':
    $tweetFileContent = $block-&gt;tweet-&gt;one()-&gt;getContents();
    $tweetJson = json_decode($tweetFileContent, true);
    $tweetUrl = $tweetJson['url'];
  
    if ($tweetUrl) {
      $components[] = [
        'role' =&gt; 'tweet',
        'layout' =&gt; 'tweetLayout',
        'URL' =&gt; $tweetUrl,
      ];
    }
  break;
  </code></pre>
  <p>However, I would recommend extending your code as follows to address empty state and potential exception:</p>
  <pre><code>case 'tweet':
    if (empty($block-&gt;tweet)) {
      // return early or do something else
    }
    try {
      $tweetFileContent = $block-&gt;tweet-&gt;one()-&gt;getContents();
      $tweetJson = json_decode($tweetFileContent, true);
      $tweetUrl = $tweetJson['url'];
  
      if ($tweetUrl) {
        $components[] = [
          'role' =&gt; 'tweet',
          'layout' =&gt; 'tweetLayout',
          'URL' =&gt; $tweetUrl,
        ];
      }
    } catch ($e) {
      // getContents() throws 
      // InvalidConfigException if [[volumeId]] is missing or invalid,
      // AssetException if a stream could not be created
    }
  break;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-19T17:50:55.083Z
Answers.Text: This did it! Thanks so much 
Answers.UserDisplayName: Bryce
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-18T17:54:15.223Z
Answers.LastActivityDate: 2020-10-18T17:54:15.223Z
Answers.UserDisplayName: Johannes
Body: <p>I'm using the Apple News plugin to try and display a tweet that's been added as a JSON asset in a matrix field. The JSON looks something like this</p>
  <pre><code>{
      &quot;title&quot;: &quot;https://twitter.com/TehseenLadha/status/1291773476877959169&quot;,
      &quot;description&quot;: null,
      &quot;url&quot;: &quot;https://twitter.com/TehseenLadha/status/1291773476877959169&quot;,
      ...
  }
  </code></pre>
  <p>According to the <a href="https://developer.apple.com/documentation/apple_news/tweet" rel="nofollow noreferrer">Apple News Publisher</a> docs, all you need is the tweet's URL. I'm trying to get it by converting the JSON file to a string with <code>file_get_contents</code> and then using <code>json_decode</code>, but it's not working. This is what I have in MyNewsArticle.php:</p>
  <pre><code>case 'tweet':
    $asset = $block-&gt;tweet-&gt;one()-&gt;getUrl();
    $string = file_get_contents($asset);
    $jsonArr = json_decode($string, true);
    $tweetUrl = $jsonArr['url'];
  
    if ($tweetUrl) {
      $components[] = [
        'role' =&gt; 'tweet',
        'layout' =&gt; 'tweetLayout',
        'URL' =&gt; $tweetUrl,
      ];
    }
  break;
  </code></pre>
  <p>It's grabbing the correct URL of the asset, but fails with <code>failed to open stream: Operation timed out</code> on the <code>$string = file_get_contents($asset);</code> line. Apparently this is something to do with IPv4 vs IPv6? I'm not certain how to get around it though.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-13T06:16:01.353Z
Comments.Text: I’m voting to close this question because it isn't Craft specific. Stack Overflow might be a better place to ask.
Comments.UserDisplayName: Simon Kuran
Tags: <php><json><plugin-applenews>
Title: Get a key value from a JSON asset in PHP
ThreadText: # Get a key value from a JSON asset in PHP
  
  (Asked by: username_0 on 2020-10-08)
  
  I'm using the Apple News plugin to try and display a tweet that's been added as a JSON asset in a matrix field. The JSON looks something like this
  
  ```
  {
      "title": "https://twitter.com/TehseenLadha/status/1291773476877959169",
      "description": null,
      "url": "https://twitter.com/TehseenLadha/status/1291773476877959169",
      ...
  }
  
  ```
  
  According to the [Apple News Publisher](https://developer.apple.com/documentation/apple_news/tweet) docs, all you need is the tweet's URL. I'm trying to get it by converting the JSON file to a string with `file_get_contents` and then using `json_decode`, but it's not working. This is what I have in MyNewsArticle.php:
  
  ```
  case 'tweet':
    $asset = $block->tweet->one()->getUrl();
    $string = file_get_contents($asset);
    $jsonArr = json_decode($string, true);
    $tweetUrl = $jsonArr['url'];
  
    if ($tweetUrl) {
      $components[] = [
        'role' => 'tweet',
        'layout' => 'tweetLayout',
        'URL' => $tweetUrl,
      ];
    }
  break;
  
  ```
  
  It's grabbing the correct URL of the asset, but fails with `failed to open stream: Operation timed out` on the `$string = file_get_contents($asset);` line. Apparently this is something to do with IPv4 vs IPv6? I'm not certain how to get around it though.
  
  **Tags:** php, json, plugin-applenews
  
  > **Comments:**
  > **username_1**: I’m voting to close this question because it isn't Craft specific. Stack Overflow might be a better place to ask.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-10-18. Score: 2 | Accepted answer
  
  I assume that `$block->tweet->one()` returns an Asset element in your matrix block called `tweet`. I suggest that you use the `getContents()` method on Craft's Asset element type to retrieve the string content of the actual file. Your code block would look something like this:
  
  ```
  case 'tweet':
    $tweetFileContent = $block->tweet->one()->getContents();
    $tweetJson = json_decode($tweetFileContent, true);
    $tweetUrl = $tweetJson['url'];
  
    if ($tweetUrl) {
      $components[] = [
        'role' => 'tweet',
        'layout' => 'tweetLayout',
        'URL' => $tweetUrl,
      ];
    }
  break;
  
  ```
  
  However, I would recommend extending your code as follows to address empty state and potential exception:
  
  ```
  case 'tweet':
    if (empty($block->tweet)) {
      // return early or do something else
    }
    try {
      $tweetFileContent = $block->tweet->one()->getContents();
      $tweetJson = json_decode($tweetFileContent, true);
      $tweetUrl = $tweetJson['url'];
  
      if ($tweetUrl) {
        $components[] = [
          'role' => 'tweet',
          'layout' => 'tweetLayout',
          'URL' => $tweetUrl,
        ];
      }
    } catch ($e) {
      // getContents() throws 
      // InvalidConfigException if [[volumeId]] is missing or invalid,
      // AssetException if a stream could not be created
    }
  break;
  
  ```
  
  > **Comments:**
  > **username_0**: This did it! Thanks so much   
  
  ---
  
  