我必须使用REST Api获取数据,而我的响应是在JSON中。在解码响应是一个数组后,我必须在网页中显示它,这样数组中的每一个响应都以特定的显示格式显示(就像twitter中的tweets以特定的框类型格式显示一样)。如何在php中循环遍历数组并显示它?我找到的唯一解决方案是使用echo将HTML嵌入到php中,但这并不奏效,调试器也没有太大帮助。我错过了什么?
请指教该怎么做。
下面是我的代码:
echo "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890" data-name="" data-user-id="2665607941" data-expanded-footer="<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow="false"data-you-block="false">" ;
echo "<div class="content">" ;
echo "<div class="stream-item-header">" ;
echo "<a class="account-group js-account-group js-action-profile js-user-profile-link js-nav" data-user-id="2665607941">
<img class="avatar js-action-profile-avatar" src="https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg" alt="">
<strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part> </strong>
<span>‏</span><span class="username js-action-profile-name" data-aria-label-part><s></s><b> </b></span>
</a>" ;
echo "<small class="time">
<a class="tweet-timestamp js-permalink js-nav js-tooltip" title="11:00 PM - 25 Sep 2014" ><span class="_timestamp js-short-timestamp "
data-aria-label-part="last" data-time="1411711218" data-time-ms="1411711218000" data-long-form="true">Sep 25</span></a>
</small>" ;
echo"</div>";
echo "<p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0"> </p>" ;
echo "<div class="stream-item-footer">";
echo "<ul class="tweet-actions js-actions">";
echo "<li class="more-tweet-actions">";
echo "<div class="action-more-container">";
echo "<div class="dropdown">";
echo "<button type="button" class="btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip">";
echo "<span class="Icon Icon--delete"></span>";
echo "<b>Delete</b>";
echo "</button>";
echo "<div class="dropdown-menu">";
echo "<div class="dropdown-caret">";
echo "<div class="caret-outer"></div>";
echo "<div class="caret-inner"></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
echo "</div>";
在Scott的回答之后,我改变了我的代码,但它仍然不工作,我没有得到它。
echo "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable js-original-tweet
my-tweet\“data-tweet-id=”515380319977893890\“data-item-id=\”515380319977893890\“data-name=\”data-user-id=\“2665607941\”data-expanded-footer=\“
回声“”;
echo "<div class=\"stream-item-header\">" ;
echo "<a class=\"account-group js-account-group js-action-profile js-user-profile-link js-nav\" data-user-id=\"2665607941\
">
<img class=\"avatar js-action-profile-avatar\" src=\"https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg\" alt=\"\">
<strong class=\"fullname js-action-profile-name show-popup-with-id\" data-aria-label-part> </strong>
<span>‏</span><span class=\"username js-action-profile-name\" data-aria-label-part><s></s><b> </b></span>
</a>" ;
echo "<small class=\"time\">
<a class=\"tweet-timestamp js-permalink js-nav js-tooltip\" title=\"11:00 PM - 25 Sep 2014\" ><span class=\"_timestamp js-short-timestamp \"
data-aria-label-part=\"last\" data-time=\"1411711218\" data-time-ms=\"1411711218000\" data-long-form=\"true\">Sep 25</span></a>
</small>" ;
echo"</div>";
echo "<p class=\"js-tweet-text tweet-text\" lang=\"en\" data-aria-label-part=\"0\"> </p>" ;
echo "<div class=\"stream-item-footer\">";
echo "<ul class=\"tweet-actions js-actions">";
echo "<li class=\"more-tweet-actions\">";
echo "<div class=\"action-more-container\">";
echo "<div class=\"dropdown\">";
echo "<button type=\"button" class=\"btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip\">";
echo "<span class=\"Icon Icon--delete\"></span>";
echo "<b>Delete</b>";
echo "</button>";
echo "<div class=\"dropdown-menu\">";
echo "<div class=\"dropdown-caret\">";
echo "<div class=\"caret-outer\"></div>";
echo "<div class=\"caret-inner\"></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
echo "</div>";
检查你的单引号和双引号。正在返回的字符串正在被截断。这是无效的:
echo "<div class="stream-item-header">" ;
将其更改为:
echo "<div class=\"stream-item-header\">" ;
或:
echo "<div class='stream-item-header'>" ;
并对每条回波线重复此修复。
例如:
echo "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890" data-name="" data-user-id="2665607941" data-expanded-footer="<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow="false"data-you-block="false">" ;
变成这样:
echo "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile- popup-actionable js-original-tweet my-tweet\" data-tweet-id=\"515380319977893890\" data-item-id=\"515380319977893890\" data-name=\"\" data-user-id=\"2665607941\" data-expanded-footer=\"<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> \" data-you-follow=\"false\" data-you-block=\"false\">" ;
对于一个更高级的技巧,您可以将整个HTML移动到一个单独的文件中,如果您愿意,也可以将其移动到一个HTML模板中。然后通过PHP读取文件内容,根据需要查找/替换变量或动态部分。但这可能超出了你的问题范围。