I am trying to output specific information coming from JIRA, I have JSON looping and results showing, but the results furthest down the nest are only showing the first character.
I have tried following the suggestions here but this seems to be for results at the top level and I can't seem to get it working with my code.
<?php
$username = '*************';
$password = '*************';
$url = '<JIRA-DOMAIN>/rest/api/2/search?jql=project=ICI&fields=key,summary,description&maxResults=10';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$issue_list = (curl_exec($curl));
$json = json_decode($issue_list, TRUE);
?>
<div class="toto">
<?php
foreach($json['issues'] as $obj){ ?>
<h2><?php echo $obj['id']; ?></h2>
<?php foreach($obj['fields'] as $parc){ ?>
<a href="<?php echo $parc['summary']; ?>">
<span><?php echo $parc['description']; ?></span>
</a>
<?php }
} ?>
</div>
When I echo $issue_list I get the following:
Output Shortened
{
"expand":"schema,names",
"startAt":0,
"maxResults":10,
"total":9,
"issues":[
{
"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id":"10058",
"self":"<domain>/rest/api/2/issue/10058",
"key":"ICI-9",
"fields":{
"summary":"ToS and PP button removal",
"description":"The Terms of Service and Privacy Policy buttons go to a page on the website so they can't use the app after. They also go to a page of nothing because they aren't written so they need to be deleted"
}
},
{
"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id":"10048",
"self":"<domain>/rest/api/2/issue/10048",
"key":"ICI-1",
"fields":{
"summary":"Create Launch Image",
"description":"Create launch image for app. Resolution should be: 2048px × 1536px\r\n\r\n[~james.whitaker] I got this info from [here|https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/]"
}
}
]
}
But the loop output only shows the first character for the Description and Summary as seen below but shows the entire ID.
Any help would be highly appreciated.
Your problem is in this foreach loop:
<?php foreach($obj['fields'] as $parc){ ?>
<a href="<?php echo $parc['summary']; ?>">
<span><?php echo $parc['description']; ?></span>
</a>
<?php }
$parc will have the value of $obj['fields']['summary'] and then $obj['fields']['description'] in the loop, so when you try and index it with ['summary'] you are using an invalid index which is translating to 0 (hence the single character). Try this instead (remove the foreach):
<a href="<?php echo $obj['fields']['summary']; ?>">
<span><?php echo $obj['fields']['description']; ?></span>
</a>
i think fields isn't array.
Change $parc['summary'] to $obj['fields']['summary']
Related
This code that I took form another web, works! but only when I put my Instragram ID. Otherwise when I want to get a media from another user (changing the variable userid) it stops working.
To be sure I use https://www.otzberg.net/iguserid/index.php to get the user ID.
And to get the Access Token (only with my account): http://instagram.pixelunion.net/
<?php
// Supply a user id and an access token
$userid = "-----";
$accessToken = "---";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php if(!empty($result->data)): ?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
<?php endif ?>
The link you posted clearly indicates that the token only works with your own images.
In order to display your Instagram photos on your own website,
you are required to provide an Instagram Access Token.
You should try a different API or method to show images from another user.
I'm using PHP and cURL to retrieve API information from a website. I have a loop to display all the information on my page which works just fine. The problem I'm having is, my results page displays 10 items per page. The API offers pagination in which it provides me with the next page link url to page 2, 3, and so forth. I cannot find for the life of me, how to make this next link load page 2 information and display it on the same page(it sounds easy enough). I have tried numerous different techniques and none have worked so far. I'm currently stuck where page 1 loads and displays page 1 and 2 data. I would like to have it to where you click the next link and it refreshes the page with your page 2 data and removes page 1 data. Any help is greatly appreciated...Here is my current code...
<section>
<form method="post" action="<?php echo $PHP_SELF;?>" id="searchForm">
<input type="text" name="searchText" id="searchText" placeholder="e.g. Haze.." value=""/><br>
<button type="submit" name="submit" id="searchButton" form="searchForm">Search</button>
<?php
if (isset($_POST['submit'])){
$url = 'https://www.cannabisreports.com/api/v1.0/strains/search/:' . urlencode ($_POST["searchText"]);
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-API-Key : XXXXXXXXXXXXXXXXXXXXXXXX'));
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($curl);
curl_close($curl);
$jsonobj = json_decode($result);
echo('<ul id="resultList">');
foreach($jsonobj->data as $value)
{
echo('<li class="resultListItem">');
echo('<img src="' . $value->image . '">');
echo '<br>';
echo $value->seedCompany->name;
echo '<br>';
echo $value->genetics->names;
echo '<br>';
echo $value->reviews->count;
echo("</li>");
}
echo("</ul>");
//********code for next page
echo(' Next ');
if(isset($_GET['nextPage'])) {
header('location: http://www.strains.blazedmaps.com ');
$urlNext = $jsonobj->meta->pagination->links->next;
$curlNext = curl_init();
curl_setopt ($curlNext, CURLOPT_URL, $urlNext);
curl_setopt ($curlNext, CURLOPT_HTTPheader, array('X-API-Key : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'));
curl_setopt ($curlNext, CURLOPT_RETURNTRANSFER, 1 );
$resultNext = curl_exec($curlNext);
curl_close($curlNext);
$jsonobjNext = json_decode($resultNext);
echo('<ul id="resultList">');
foreach($jsonobjNext->data as $value1)
{
echo('<li class="resultListItem">');
echo('<img src="' . $value1->image . '">');
echo '<br>';
echo $value1->seedCompany->name;
echo '<br>';
echo $value1->genetics->names;
echo '<br>';
echo $value->reviews->count;
echo("</li>");
}
echo("</ul>");
}
}
?>
</form>
</section>
I am going to pull my hair out. Can anyone please help me get this to work I am sure it's something stupid.. I have got all the PHP errors to go away, but I can not get images to show up. Code below...
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="jquery.fancybox-1.3.4.css" type="text/css">
<script type='text/javascript' src='jquery.min.js'></script>
<script type='text/javascript' src='jquery.fancybox-1.3.4.pack.js'></script>
<script type="text/javascript">
$(function() {
$("a.group").fancybox({
'nextEffect' : 'fade',
'prevEffect' : 'fade',
'overlayOpacity' : 0.8,
'overlayColor' : '#000000',
'arrows' : false,
});
});
</script>
<?php
// Supply a user id and an access token
$userid = "1d458ab0c149424c812e664c32b48149";
$accessToken = "c195717e379f48c68df451cc3d60524a";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php if(!empty($result->data)): ?>
<?php foreach ($result->data as $post){ ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php } ?>
<?php endif ?>
</html>
What you need is to add some checks at various points to find out what is coming back from Instagram and handle any issues. While debugging, sticking var_dump() all over the place can be helpful to see where issues lie.
Here is an example of your PHP section with some additional checks:
<?php
// Supply a user id and an access token
$userid = "USER ID";
$accessToken = "ACCESS TOKEN";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
// Check a response was returned
if ($info['http_code'] == '404') {
echo ('Error: HTTP 404 returned, bad request');
die();
}
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/?access_token={$accessToken}");
$result = json_decode($result);
// Check the json_decode succeeded
if (empty($result)) {
echo "Error: JSON not returned from API";
die();
}
// Check no error was returned from Instagram
if ($result->meta->code != 200) {
echo "Error: ".$result->meta->error_message;
die();
}
?>
If you plan on doing a lot of work with the Instagram API, you may want to look at a library to do most of the heavy lifting. This one appears to be the most popular at present.
i am using the code below, to show some photos of my instagram-account on my website. it just fetches all the images of my account in the div. Is there a way to limit the fetched data to 10 Images or so ?
Cant figure out how to do that..
thanks for your help!
<div id="instagramfeed">
<?php
// Supply a user id and an access token
$userid = "123xy";
$accessToken = "123xy ";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. #Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?> <br><br><br><br>
</div>
You could use the count parameter.
https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}&count=10
It was a problem in Instagram Developer Console. max_id and min_id doesn't work there.
For anyone interested - i found a solution for this problem:
it doesnt work with: {$accessToken}&count=10
But it works with:
?access_token=123456789101112131415123123111&count=10
I know this has been done to death.
but i am really strugling
I have put together a webservice that generates a json,
i can and understand this bit in
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://marcom.domain.com/corp/pub.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
this will dump out on the page [{"Torders":"3222","name":"john"},{"Torders":"579","name":"Kevin"}]
their is 5 in total but for this i am keeping it simple
I don't understand how to get these as an array so that my end result is a list
of name and Torders
the rendered html would be something like this
<li>john 3222</li>
<li>Kevin 579 </li>
please don't send me to php manual page for json decode cause i am strugling to understand this.
thank you
<?php
$json = file_get_contents('http://marcom.domain.com/corp/pub.php');
$people = json_decode($json);
?>
<ul>
<?php foreach ($people as $person): ?>
<li><?=$person->name?> <?=$person->Torders?></li>
<?php endforeach; ?>
</ul>
If you want to use curl, you'll want to set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); and get the return from curl_exec()
U need to use json_decode()
$j = '[{"Torders":"3222","name":"john"},{"Torders":"579","name":"Kevin"}]';
$data = json_decode($j,true);
//print_r($data);
echo '<ul>';
foreach($data as $key=>$val){
echo '<li>'.$val['name'].' '.$val['Torders'].'</li>';
}
echo '</ul>';