Okay, so here's what I'm trying to achieve!
I have a PHP script that I pull information into a foreach() statement from an API/JSON setup.
This is some of my code:
$ch = curl_init();
$category=$_GET['category'];
$url="http://www.someurl.com/api/listings?token=f716909f5d644fe3702be5c7895aa34e&group_id=10046&species=".$category;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: f716909f5d644fe3702be5c7895aa34e',
));
$json = json_decode(curl_exec($ch), true);
// Functions relating to the Echo Code
foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://www.someurl.com/?variable=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
Then, I'm creating the image to display with the following code:
// Echo the output
echo'<div class="animal">
<div class="animal-image">';
if ($listing['photos'] > 1) {
echo '<a class="size-thumbnail thickbox" rel="gallery" href="'.$medium_photo.'">
<img src="'.$medium_photo.'" class="image-with-border" alt="">
<div class="border" style="width: 340px; height: 340px;">
<div class="open"></div>
</div>
</a>';
}
else {
echo '<a class="size-thumbnail thickbox" href="'.$medium_photo.'">
<img src="'.$medium_photo.'" class="image-with-border" alt="">
<div class="border" style="width: 340px; height: 340px;">
<div class="open"></div>
</div>
</a>';
}
So, for example, the API pulls in 25 items that are all formatted into their individual section, each section has it's own initial photo. When I click on that photo, the lightbox loads.
BUT, it loads the lightbox for ALL images in the API call, so I'm getting 144 images available in the lightbox.
Is there a way to limit the photos in the lightbox for each item that's being retrieved?
Any help would be appreciated.
this answer is based on my assumption that you use thickbox plugin (if thats the case please update your question details :D )
if you want every image to be "alone" and not in a gallery then every link must have a different rel="name-of-individual-image-gallery-whatever"
so you can do something like
// Functions relating to the Echo Code
// add a counter
$inc=0;
foreach($json['listings'] as $listing)
{
$short_personality=substr($listing['personality'],0,500);
$peturl="http://www.someurl.com/?variable=".$listing['id'];
$medium_photo=$listing['photos'][0]['large_340'];
$gender_class=strtolower($listing['gender']);
$breed_class=strtolower($listing['species']);
$name=($listing['name']);
// add this
$unique_gallery_name="individual-gallery-".$inc;
$inc++;
// the rest of your code...
// .
// .
// .
and then add that rel attribute to your output code
<a class="size-thumbnail thickbox" rel="'.$unique_gallery_name.'"
so each link will have one rel like this rel="individual-gallery-0", the next link rel="individual-gallery-1", the next link rel="individual-gallery-2" ... and so on. The result will be every image to be in a "unique" gallery, "alone"
Related
I'm trying to show a facebook feed on my website, which is working. But I only managed to show the text of the post, not the image attached to it (or if possible, if there are multiple images, only the first one or biggest one).
I tried looking here for the correct name to get it using the API
This is my code now (which shows facebook posts in an owl carousel):
function fetchUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// You may need to add the line below
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$feedData = curl_exec($ch);
curl_close($ch);
return $feedData;
}
//App Info, needed for Auth
$app_id = "1230330267012270";
$app_secret = "secret";
//Retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=1230330267012270&client_secret=secret");
$json_object = fetchUrl("https://graph.facebook.com/267007566742236/feed?{$authToken}");
$feedarray = json_decode($json_object);
foreach ( $feedarray->data as $feed_data )
{
if($feed_data->message != ''){
$facebookfeed .= '
<div class="item">
<div class="product-item">
<img class="img-responsive" src="images/siteimages/imgfacebook.jpg" alt="">
<h4 class="product-title">'.$feed_data->name.'</h4>
<p class="product-desc">'.$feed_data->message.'</p>
<p>'.$feed_data->story.'</p>
<img src="'.$feed_data->picture.'">
<p>Lees meer</p>
</div><!-- Product item end -->
</div><!-- Item 1 end -->';
}
}
echo $facebookfeed;
Looking at the Facebook documentation I thought $feed_data->picture would work, but it returns nothing.
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data.
Source: https://developers.facebook.com/docs/apps/changelog#v2_4
$url = "http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1";
$html = file_get_contents($url);
This returns some of the page content. I think that because the page I am trying to curl uses jquery to insert the listings the curl executes before the jquery populates the page?
Any ideas on how to get the full contents of the search curl?
Try this:
$url = 'http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1';
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($url);
echo $returned_content;
No they are not getting the listings via jQuery, AJAX. There are no XHR requests.
A view source shows the listings.
I just used this code to get
header('Content-Type: text/plain; charset=utf-8');
$url = 'http://www.ksl.com/index.php?nid=231&sid=74268&cat=&search=&zip=&distance=&min_price=&max_price=&type=&category=&subcat=&sold=&city=&addisplay=%5BNOW-1HOURS+TO+NOW%5D&sort=1&userid=&markettype=sale&adsstate=&nocache=1&o_facetSelected=true&o_facetKey=ad+posted&o_facetVal=Last+Minute&viewSelect=list&viewNumResults=48&sort=1';
$data = file_get_contents($url);
echo $data;
The Result
Partial edited
Black Xbox 360 320gb Hard Drive Plus controllers and G...</a>
<span style="color: #555;">Magna, UT
| 1 Min
<div class="adDesc">
Selling because I got it used then only used it once, works fantastically and comes with lots of games plus 4 controllers and a keyboard for the contr
<a class="listlink" href="?nid=218&ad=34170943&cat=&lpid=&search=&ad_cid=7">more...</a>
<div class="priceBox">
<a href="?nid=218&ad=34170943&cat=&lpid=&search=&ad_cid=7">
<span >$450<span class="priceCents">00
<!-- <div class="adDays">1 Min -->
<!--<div class="adTime">Dec 31, 1969-->
<div class="detailBox">
Great PS4 Bundle</a>
<span style="color: #555;">Spanish Fork, UT
| 1 Min
Have here a great PS4 bundle with 6Games 1controller and 3months of PS Network. Still has the box. Text 801-633-1659
<span >$400<span class="priceCents">00
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
First post here so apologies in advance if this is an incorrect format. I am working with the Instagram API to pull images. The Instagram API only returns 1 page of images at a time, but offers pagination and next_url to grab the next page of images. When I use the function fetchInstagramAPI below, to grab only the first page, the php code works fine.
When I attempt to use the loopPages function together with the fetchInstagramAPI function, to try and grab all pages at once, I receive the error "Using $this when not in object context". Any idea? Thank you for the help in advance.
Function fetchInstagramAPI gets our data
<?php
function fetchInstagramAPI($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$contents = curl_exec($ch);
curl_close($ch);
return json_decode($contents);
}
Function loopPages uses pagination and next_url to grab all pages of images
function loopPages($url){
$gotAllResults = false;
$results = array();
while(!$gotAllResults) {
$result = $this->fetchInstagramAPI($url);
$results[] = $result;
if (!property_exists($result->pagination, 'next_url')) {
$gotAllResults = true;
} else {
$url = $result->pagination->next_url;
}
}
return $results;
}
This pulls, parses, then displays the images in a browser
$all_url = 'https://api.instagram.com/v1/users/{$userid}/media/recent/?client_id={$clientid}';
$media = loopPages($all_url);
foreach ($media->data as $post): ?>
<!-- Renders images. #Options (thumbnail, low_resoulution, standard_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
In PHP and many object oriented languages $this is a reference to the current object (or the calling object). Because your code don't seem to be in any class $this doesn't exists. Check this link for PHP classes and objects.
Since you have just defined your functions in the file you can try calling the function with $result = fetchInstagramAPI($url); (without $this).
edit:
For foreach check if $media->data is in fact an array and try another syntax which i think is easier to read.
edit2:
Since you now know how your $media looks like you can wrap around another foreach loop that will iterate through the pages:
foreach ($media as $page){
foreach ($page->data as $post) {
echo '<!-- Renders images. #Options (thumbnail, low_resoulution, standard_resolution) -->';
echo '<a class="group" rel="group1" href="' . $post->images->standard_resolution->url . '"><img src="' . $post->images->thumbnail->url . '"></a>';
}
}
I need a script that accepts this ul:
<ul id="activitylist">
<li class="activitybit forum_thread">
<div class="avatar"> <img alt="secret team's Avatar" src="images/misc/unknown.gif" title="secret team's Avatar"> </div>
<div class="content hasavatar">
<div class="datetime"> <span class="date">Today, <span class="time">11:25pm</span></span> </div>
<div class="title"> <a class="username" href="member.php/436070-secret-team">secret team</a> started a thread 'Allow [VIDEO] Code' missing in settings </div>
<div class="views">0 replies | 0 view(s)</div>
</li>
</ul>
There are 10 to 15 child li in one ul. I need thread name of every child li where thread has 0 replies. I posted one example li above. So for that example I need this text:
'Allow [VIDEO] Code' missing in settings
where this div has 0 replies as a text:
<div class="views">0 replies | 0 view(s)</div>
I have this sample code but it is not working correctly.
<?php
$request_url = 'https://www.vbulletin.com/forum/activity.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url); // The url to get links from
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone
$result = curl_exec($ch);
$sPattern = "/<li class=\"activitybit forum_thread\">(.*?)<\/li>/s";
preg_match_all($sPattern, $result, $parts);
$links = $parts[1];
foreach ($links as $link) {
if (stripos($link, "0 replies") !== false) {
echo $link . "<br>";
}
}
curl_close($ch);
?>
Here is a regex that will parse any kind of HTML:
$regex = new DOMDocument;
$regex->loadHTML($html);
Now serious. DOMDocument has parsed all of your HTML. You can now use these and these functions to walk over tags and extract their attribute and contents. But it is much easier to use a companion class called DOMXPath:
$xpath = new DOMXpath($regex);
foreach ($xpath->query("//ul[#id='activitylist']/li") as $li) {
$view = $xpath->query(".//div[#class='views']", $li)->item(0);
$link = $xpath->query(".//div[#class='title']/a", $li)->item(1);
if (preg_match("/0 replies/", $view->nodeValue)) {
echo $link->nodeValue . " (" . $link->getAttribute("href") . ")\n";
}
}
This will output few warnings about your HTML not being perfect plus this:
'Allow [VIDEO] Code' missing in settings (showthread.php/415403-Allow-VIDEO-Code-missing-in-settings)
You can read more about using Regex PHP to parse HTML here. A comprehensive list of XPath examples is available here.