简体   繁体   English

cURL:foreach循环中的变量

[英]cURL: variable in foreach loop

Good day, can anyone help me to figure out what is wrong in my code or if I coded it the wrong way. 美好的一天,任何人都可以帮助我找出代码中的错误之处,或者如果我用错误的方式进行了编码。

The curl part is ok my problem is when I started to get the file using foreach loop the result is broken image. 卷曲部分是好的,我的问题是当我开始使用foreach循环获取文件时,结果是损坏的图像。

I've try it in array but nothings happen. 我已经尝试过数组,但没有任何反应。 I'm new with this, maybe I'm missing something here 我对此很陌生,也许我在这里错过了一些东西

Here is my code: 这是我的代码:

    <?php 
$url = "http://XXXXXXXXXXXXXX"; //Base Url
$parameters = ['mode' => 'contributors'];  // riders, current_rounds, contributors, season_entries
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch,CURLOPT_HTTPHEADER, ['x-weplaymedia-authorisation:XXXXXXXXXXXXX']);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch); // Execute

$arr = json_decode($result,true); // Dump result here.

//print_r($arr);

If you run print_r($arr); 如果运行print_r($arr); it will display array of fields. 它将显示字段数组。

But when I try to point certain fields ( [fwcContributors] ) in my foreach loop code im getting a broken images. 但是当我尝试在我的foreach循环代码中指向某些字段( [fwcContributors] )时,我得到的图像就坏了。

Here is the image of array: 这是数组的图像:

这是数组的图像

Here is the result 这是结果

这是结果

What I want is to display their profile picture from [profilePicture] and username from [userName] . 我要显示的是[profilePicture]中的个人资料图片和[userName]中的用户名。

$i=0;
foreach ($arr['fwcContributors'] as $val)
{
if($i++ == 5);
echo '<tbody >';
echo '<tr style="transform: skewX(-20deg);">';
echo    '<td>';
echo '<img src='.($val['profilePicture']) .' style="transform:  skewX(20deg);">' . htmlspecialchars($val['userName']);
echo    '</td>';
echo '</tr>';
}

    ?>

Thank you in advance. 先感谢您。

There are nested arrays in fwcContributors , of which you probably want ContributorList to iterate over: fwcContributors有嵌套的数组,您可能希望ContributorList对其进行迭代:

foreach ($arr['fwcContributors']['ContributorList'] as $val)
{
    echo '<tbody >';
    echo '<tr style="transform: skewX(-20deg);">';
    echo '<td>';
    echo '<img src='.($val['profilePicture']) .' style="transform: skewX(20deg);">' . htmlspecialchars($val['userName']);
    echo '</td>';
    echo '</tr>';
}

(Took the $i statements out, as they don't seem to do anything.) (将$i语句删除,因为它们似乎没有任何作用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM