在foreach循环中,我试图与来自两个变量的数据建立链接。喜欢这个:
$baseAttachementUrl = str_replace($rowAttachTags, $rowAttachVals, $getAttachmentURL);
foreach ($attachments as $attachment){
$rowAttachTags = array('{{ROWID}}', '{{SHEETID}}','{{ATTACHMENTID}}');
$rowAttachVals = array($addRowsObj->result[0]->id, $theSheet->id, $attachment->id);
$getAttachmentURL = $baseAttachementUrl;
$getNEWAttachmentURL = str_replace($rowAttachTags, $rowAttachVals, $getAttachmentURL);
$curlSession = curl_init($getNEWAttachmentURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
getAttachmentResponse = curl_exec($curlSession);
$getFileObj = array(json_decode($getAttachmentResponse, true));
foreach ($getFileObj as $fileObj){
echo "<a href='". $fileObj['url']."'>". $fileObj['name']."</br></a>";
}
}
表中的第一行有效。 2 个文件带有正确的 URL。 但是在所有后续行中,第一行中的最后一个文件都出来了。
这里有几个问题,你不太具体。但是,我会尽力回应。
首先,我们不知道$attachments
或$getFileObj
的结果是什么样的。我们假设您的问题中说"返回 stdClas..."是 $getFileObj
结果的一部分。
另一个问题是,我们不知道你从哪里得到$getAttachmentURL
。这是一个静态设置的变量,还是由于$attachments
而产生的变量?
考虑到这些问题,我相信您要做的是首先获取所有附件并将它们存储到$attachments
中。然后,您希望从初始结果中获取每个附件 URL,并遍历每个附件 URL 以获取您的 fileURL。我会做以下几点:
<?php
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getAttachmentsResponse = curl_exec($curlSession);
// Assign response to variable
$attachments = json_decode($getAttachmentsResponse);
foreach ($attachments as $attachment) {
$getAttachmentURL = $attachment->attachment_URL;
$curlSession = curl_init($getAttachmentURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getAttachmentResponse = curl_exec($curlSession);
// Assign response to variable
$getFileObj = json_decode($getAttachmentResponse);
echo "<td>";
echo "<a href='" . $getFileObj->url . "'>" . $attachment->name . "</br></a>";
echo "</td>";
}
试试....
// ... curl setup
$attach = json_decode($getAttachmentsResponse);
// ... curl setup two
$file = json_decode($getAttachmentResponse);
$joinedattachment[ 'attach' ] = $attachone;
$joinedattachment[ 'file' ] = $file;
$attachments[] = $joinedattachment;
$iCountAttachments = count( $attachments );
echo "<td>";
for( $i = 0; $i < $iCountAttachments; ++$i )
{
echo "<a href='". $attachments[ $i ][ 'file' ]."'>". $attachments[ $i ][ 'attach' ] ."</br></a>";
}
echo "</td>";