我必须使用REST Api获取数据,而我的响应是在json中。解码后,响应是在一个数组中,我必须在网页中显示它,以便阵列中的每个响应以特定的显示格式显示(Just Like tweet in twitter以特定的盒子类型格式显示)。如何在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>";
在斯科特的回答之后,我改变了我的代码,但它仍然没有用,而我却没有得到它。
echo "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable js-original-tweet
我的鸣叫\&#34;数据鸣叫-ID =&#34; 515380319977893890 \&#34;数据项-ID = \&#34; 515380319977893890 \&#34;数据名称= \&#34; \&#34;数据用户ID = \&#34; 2665607941 \&#34; data-expanded-footer = \&#34;&lt; div class =&#34; js-tweet-details-fixer tweet-details-fixer&#34;&gt; &lt; div class =&#34; entities-media-container js-media-container&#34;风格=&#34;最小高度:0像素&#34;&GT; &LT; / DIV&GT; &lt; div class =&#34; js-machine-translated-tweet-container&#34;&gt;&lt; / div&gt; &lt; div class =&#34; js-tweet-stats-container tweet-stats-container&#34;&gt; &LT; / DIV&GT; &lt; div class =&#34; client-and-actions&#34;&gt; &lt; span class =&#34;元数据&#34;&gt; &lt; span&gt; 11:00 PM - 2014年9月25日&lt; / span&gt; &安培; middot; &lt; a class =&#34; permalink-link js-permalink js-nav&#34; HREF =#&34; /状态/ 515380319977893890&#34;的tabindex =&#34; -1&#34;&GT;详情&LT; / A&GT; &LT; /跨度&GT; &LT; / DIV&GT; &LT; / DIV&GT; &#34; 数据你遵循= \&#34;假\&#34;数据你块= \&#34;假\&#34;&GT;&#34; ;
回声&#34;&#34; ; 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>";
答案 0 :(得分:2)
检查单引号和双引号。正在返回的字符串被截断。这是无效的:
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读取文件内容,并根据需要查找/替换变量或动态部分。但是,这可能超出了您的问题的范围。
答案 1 :(得分:1)
回声中的引号需要转义:
echo "<div class=\"stream-item-footer\">";
答案 2 :(得分:1)
使用\
转义引号 echo "<div class=\"content\">" ;
答案 3 :(得分:1)
适合我:
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>';
答案 4 :(得分:1)
我在这里写下我的回答老兄,希望我帮助你。但是斯科特说,没有什么新东西,你错过了很多双引号和恕我直言,你是一个疯狂的男孩手动(很好的意义)。
带上你的JSON数组,并以这样的方式使用我在评论中链接的功能(希望你有类似的情况,在其他情况下是相同的,但你必须复杂的代码,也许):
....
for($i=0; $i<sizeof($jsonarray); $i++){
echo addslashes($jsonarray[$i]);
}
....
通过这种方式,您的所有代码都被转义,如果错误仍然存在,则意味着问题出在其他地方