TMDB API PHP - Show All and Genre - php

<?php
$ca = curl_init();
curl_setopt($ca, CURLOPT_URL, "http://api.themoviedb.org/3/configuration?api_key=5094e4539de46c1abd1461920f3a3fb9");
curl_setopt($ca, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ca, CURLOPT_HEADER, FALSE);
curl_setopt($ca, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ca);
curl_close($ca);
//var_dump($response);
$config = json_decode($response, true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/movie/106646-the-wolf-of-wall-street?api_key=5094e4539de46c1abd1461920f3a3fb9");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
//print_r($result);
$x=0;
while($x<=0)
{
echo("<img src='" . $config['images']['base_url'] . $config['images']['poster_sizes'][1] . $result['results'][$x]['poster_path'] . "'/>");
echo (" ". $result['results'][$x]['title'] . "<br/>");
echo ("<a target=\"_blank\" href=\"https://www.themoviedb.org/\">TMDb</a> ". $result['results'][$x]['vote_average'] . "/10 <br/>");
echo (" ". $result['results'][$x]['release_date']);
echo (" ". $result['results'][$x]['genre']);
$x++;
}
?>
I am trying to show 'The Wolf Of Wall Streets' information. This is not working and I am not too sure how to get it working Ive had a look around and it's all just a bit too confusing. Please help.
I would also like to have a page where you can select a category and then the films within that category show up (limited to 15 per page)
If anyone can help me with that I will say thank you. Thank You

First thing, you don't need the title of the movie in your API call. You can do like this :
http://api.themoviedb.org/3/movie/106646?api_key=5094e4539de46c1abd1461920f3a3fb9
This call return informations from 1 movie, so you don't need a while loop do display these informations :
echo ("<img src='" . $config['images']['base_url'] . $config['images']['poster_sizes'][1] . $result['poster_path'] . "'/>");
echo (" ". $result['title'] . "<br/>");
echo ("<a target=\"_blank\" href=\"https://www.themoviedb.org/\">TMDb</a> ". $result['vote_average'] . "/10 <br/>");
echo (" ". $result['release_date']);
echo (" ". $result['genre']);
For your list, first build a genre list with there IDs :
http://api.themoviedb.org/3/genre/movie/list?api_key=5094e4539de46c1abd1461920f3a3fb9
Then list your movies :
http://api.themoviedb.org/3/genre/28/movies?api_key=5094e4539de46c1abd1461920f3a3fb9&page_size=2
Instead of a while loop, you can do a foreach like this :
foreach ($results as $movie) {
echo '<li>'.$movie['title'].'</li>
}
You can find all details about the TMDB API here :
http://docs.themoviedb.apiary.io/reference/genres/genreidmovies/

Related

PHP cURL add pagination using foreach loop

I am using the attached script to output 100 results at a time from an XML file. The total record count e.g 1000 is held in $totalRecordCount = $oXML['total_record_count']; The user enters a search term in a form and results get outputted on same page. Each result is a link to a detail page. How do I integrate pagination in it if there's a 1000 results 1|2|3...10? I tried integrating something as per Simple pagination for foreach loop with no success however. Any help appreciated. Thanks
<?php
if (isset($_GET['submit2'])) {
$search2 = preg_replace('/\s+/', '+', $_GET["dept-keywords"]);
$sanitizeSearch2 = filter_var($search2, FILTER_SANITIZE_STRING);
echo '<b>Results: ' . $_GET["dept-keywords"] . '</b>';
$ch = curl_init();
$baseUrl = 'https://example.com/';
$templateParamNames = array('{user_id}');
$templateParamValues = array(urlencode('exl_impl'));
$baseUrl = str_replace($templateParamNames, $templateParamValues, $baseUrl);
$queryParams = array(
//info
);
$url = $baseUrl . "?" . http_build_query($queryParams);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$oXML = new SimpleXMLElement($response);
echo '<ol>';
$totalRecordCount = $oXML['total_record_count'];
$count = 0;
foreach ($oXML->user as $user) {
$first_name = $user->first_name;
$user_link = strtolower("https://example.com/" . $first_name);
echo '<li>';
echo "<a href='" . $user_link . "'> " . $first_name . " </a>" . "\r\n";
echo '</li>';
$count++;
}
if ($count == 0) {
echo '<label>Sorry, no results!</label>';
}
echo '</ol>';
curl_close($ch);
}

Binance API - Get Klines for XXX/GBP only

I am calling the Binance Klines API to get current prices.
// Get Assets - ideally I'd just like the currency name (e.g. ETH) rather ETC/BTC
$url = 'https://api.binance.com/api/v3/exchangeInfo';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
foreach($json->symbols as $symbol)
{
// Get prices for teh asset
$ccode = $symbol->symbol;
$nurl = 'https://api.binance.com/api/v3/klines?symbol=' . $ccode . '&interval=1m';
$stime = 1000*(time() - 60); // Time period is the last minute
$etime = 1000*time();
$nurl .= '&startTime=' . $stime;
$nurl .= '&endTime=' . $etime;
$ch = curl_init($nurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $nurl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$json = curl_exec($ch);
curl_close($ch);
if ( $json === "" || $json === "[]" || substr($json, 0, 8) == '{"code":' )
{
echo "Not found " . $ccode . " .. skipping <BR>";
continue;
}
$arr = explode(",", $json);
$price = $arr[4];
$tstamp1 = $arr[6];
$tstamp = gmdate("Y-m-d\TH:i:s\Z", ($arr[6]/1000));
echo $ccode . " " . $tstamp . " " . $price . "<BR>";
}
The problem is I get all combinations of coin/currency, when I only want the GBP prices for each coin. The full list about times out as it runs for over 5 minutes.
I'd like to just get the price of each coin in GBP.
How can I do that?
You're asking for all the symbols (exchangeInfo) and then getting the candle info (klines) for each symbol (= currency pair).
You can do so just for the GBP pairs by looking for GBP in the two currencies you're currently iterating on, by adding this to your foreach:
// rest of the code
foreach($json->symbols as $symbol)
{
if ($symbol->baseAsset === "GBP" || $symbol->quoteAsset === "GBP" ) {
// rest of your foreach
}
}
In your foreach you have these three properties under $symbol you can leverage:
$symbol->symbol // "ADAGBP"
$symbol->baseAsset // "ADA"
$symbol->quoteAsset // "GBP"

cant read data from epever to raspberry

I'm using Raspberry-pi to read data from epever by rs485 to usb.I'm using this PHP file to read data.
<?php
/*
* PHP EpSolar Tracer Class (PhpEpsolarTracer) v0.9
*
*/
//EPEver tracer php library
require_once 'PhpEpsolarTracer.php';
//influxDB php client library
require 'vendor/autoload.php';
//Define IP of influxDB
$host = 'localhost';
$tracer = new PhpEpsolarTracer('/dev/ttyUSBACM0');
$client = new InfluxDB\Client($host,8086,"root","root");
$db = $client->selectDB("logger");
//'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
Print "\n Realtime Data\n";
if ($tracer->getRealtimeData()) {
for ($i = 0; $i < count($tracer->realtimeData); $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8086/write?db=powerwall" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$Item = $tracer->realtimeData[$i];
$Key_Name = str_replace(" ","-",$tracer->realtimeKey[$i]);
Print str_pad($i, 2, '0', STR_PAD_LEFT) . " " . $Key_Name . " " . $Item . "\n";
curl_setopt($ch, CURLOPT_POSTFIELDS, "$Key_Name,unit=Realtime value=$Item" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec ($ch);
}
} else
print "Cannot get RealTime Data\n";
Print "\n Statistical Data\n";
if ($tracer->getStatData()) {
for ($i = 0; $i < count($tracer->statData); $i++) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8086/write?db=powerwall" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$Item = $tracer->statData[$i];
$Key_Name = str_replace(" ","-",$tracer->statKey[$i]);
$Key_Name = 'Stat-' . $Key_Name;
Print str_pad($i, 2, '0', STR_PAD_LEFT) . " " . $Key_Name . " " . $Item . "\n";
curl_setopt($ch, CURLOPT_POSTFIELDS, "$Key_Name,unit=statData value=$Item" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec ($ch);
}
} else
print "Cannot get Statistical Data\n";
?>
When I run this code I got the error message:
Cannot get Real Time Data
So then I followed the issue in here:
Modbus cant read data from epever tracer 1210a to raspberry
But I haven't do this, I got several errors.
Then I have checked the kernel version of my raspberry pi and it shows as
Linux raspberry pi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux

getting vimeo id codes from album

Trying to call get an array of ALL videos from a Vimeo Album. Using vimeo's JS API.
https://developer.vimeo.com/apis/simple#album-request
So the doc above says that to access information about a vimeo album you form a url like this:
http://vimeo.com/api/v2/album/album_id/request.output
and in my case that url looks like this:
$id2 = $_POST['alb1']
http://vimeo.com/api/v2/album/$id2/info.php
So everone says to use CURL to read the .php file that vimeo provides using the above url.
From it, i need to aquire the total number of videos in the album.
With that number I need to create a loop that accesses all the videos (for as long as the album is) and saves it as an array full of the video URLs.
That is then saved to a SQL database and the database is read out in a loop to print out <li>lines with an image of the video in it.
See it all in NON action here:
http://s199881165.onlinehome.us/transfem/newlayout/getal.php
here's the php I use to read the vimeo file
function getVimeoInfo($id, $info)
{
if (!function_exists('curl_init')) die('CURL is not installed!');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/album/$id/videos.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = unserialize(curl_exec($ch));
$output = $output[0][$info];
curl_close($ch);
return $output;
}
function getVimeoInfo2($id2, $info2)
{
if (!function_exists('curl_init')) die('CURL is not installed!');
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "http://vimeo.com/api/v2/album/$id2/info.php");
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
$output2 = unserialize(curl_exec($ch2));
$output2 = $output2[0][$info2];
curl_close($ch2);
return $output2;
}
Here is the other part:
$thetoatslotzes = getVimeoInfo2($posty_alby,'total_videos');
echo "<script> alert('lotze: " . $thetoatslotzes . "');</script>" ;
$albarray = getVimeoInfo($_POST['alb1'],'url');
echo "<script> alert('albarray: " . $thetoatslotzes . "');</script>" ;
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
$dbh->exec("TRUNCATE TABLE vim_playlist12");
$dbh->commit();
$i = 0;
while($i < 9) {
$eye_matee = $i + 1;
$dbh->beginTransaction();
$dbh->exec("insert into vim_playlist12 (url, listnum) values
($albarray[$i], $eye_matee)");
$i = $i + 1;
$dbh->commit();
}
$seleccion = 'SELECT url, listnum FROM vim_playlist12 ORDER BY listnum';
foreach ($dbh->query($seleccion) as $row) {
print $row['listnum'] . ": " . $row['url'] . "<br>" ;
}
}
catch (Exception $e) {
$dbh->rollBack();
echo "Failed: " . $e->getMessage();
}
}
else
{}
So ultimately if I can get that array of URLS from the vimeo videos.php i'll be in good shape.

CURL - No response and no error

I'm hoping someone could help me as I'm getting no response, no error, and no indication of why my CURL is not working here. I've searched around and added a clause to ignore the SSL (SSLVerify is set to false) and tried numerous ways to get a response.
May someone please point me in the right direction?
Thanks!
<?php
function sendContactInfo() {
//Process a new form submission in HubSpot in order to create a new Contact.
$hubspotutk = $_COOKIE['hubspotutk']; //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => 'https://www.myfoodstorage.com/onestepcheckout/',
'pageTitle' => 'MyFoodStorage.com Cart Checkout'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these varilables with values from the form.
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&email=" . urlencode($email)
. "&phone=" . urlencode($telephone)
. "&address=" . urlencode($street)
. "&city=" . urlencode($city)
. "&state=" . urlencode($region)
. "&country=" . urlencode($country)
. "&hs_context=" . urlencode($hs_context_json); //Leave this one be :)
//replace the values in this URL with your portal ID and your form GUID
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/234423/4a282b6b-2ae2-4908-bc82-b89874f4e8ed';
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
#curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
#curl_close($ch);
}
echo sendContactInfo();
echo $response;
print_r($info);
?>
1.you can't print variable value defined inside a function outside of function, like this:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
}
echo $response;
print_r($info);
but you can print value so:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
echo $response;
print_r($info);
}
sendContactInfo();
2.when you want to run function and get after value, use "return", like this:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
return $response;
//or return $info;, when you want to get array values from #curl_getinfo
}
print_r(sendContactInfo());
sendContactInfo is a function but its not being used like one.
You can't access variables that are used inside the function. Also it needs to return something
Change:
$response = #curl_exec($ch);
to
return #curl_exec($ch);

Categories