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);
}
Related
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"
<?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/
I am trying to get more than 10 results form google using the search API. I know that the google search API only gives 10 results and you have to call it 10 times to get a hundred but I can't seem to get it working. I tried creating a do while loop as well as a for loop but all it seems to do is gives me the same results over and over.
<?php
if(isset($_GET['input']) && $_GET['input'] != "")
{
echo "<br />Your Search Results Google:<br /><br />";
$i=0;
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0& key=AIzaSyBacVRiPNo7uMqhtjXG4Zeq1DtSQA_UOD4&cx=014517126046550339258:qoem7fagpyk
&num=10&start=".$i."&"."q=".str_replace(' ', '%20', $_GET['input'])
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body,true);
do
{
foreach ($json['responseData']['results'] as $data) {
echo '
<p>
', $data ['title']," ---> <u>Google SE </u>" ,'<br />
', '<a href ='.$data['url'].'>'.$data['url']."</a>" , '<br />
', $data['content'],'
</p>';
}
$i++;
}
while($i<3);
}
?>
Any input appreciated.
ok. just try the code below:
<?php
if(isset($_GET['input']) && $_GET['input'] != "")
{
echo "<br />Your Search Results Google:<br /><br />";
for ($i = 0; $i < 10; $i++)
{
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&key=AIzaSyBacVRiPNo7uMqhtjXG4Zeq1DtSQA_UOD4&cx=014517126046550339258:qoem7fagpyk
&num=10&start=".$i."&"."q=".str_replace(' ', '%20', $_GET['input']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
foreach ($json['responseData']['results'] as $data) {
echo '
<p>
', $data ['title']," ---> <u>Google SE </u>" ,'<br />
', '<a href ='.$data['url'].'>'.$data['url']."</a>" , '<br />
', $data['content'],'
</p>';
}
}
}
?>
<?php
$mysender2= "";
$myrecepient2= "923336088811,923126812536,923134126153";
$mymessage2 = "this is message";
$api_key = "****";
$api_secret="****";
$to_arr = explode(",", $myrecepient2);
foreach ($to_arr as $b){
$url = "https://rest.nexmo.com/sms/json?" .
"api_key=".$api_key. "&" .
"api_secret=" .$api_secret . "&" .
"from=" . urlencode($mysender2) . "&" .
"to=" . urlencode($b) . "&" .
"text=" .urlencode($mymessage2);
$c = curl_init($url);
// Use SSL
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
$html = curl_exec($c);
curl_close ($c);
}
?>
The above script will run again and again untill the process completed . how to show an echo on process complete .. such as "Your Process Completed"
Sorry, but, your script is so bad and your server goes cry, I know that exists 3 calls only, because your array have 3 values, but if you need more? You will have problems.
The best way in your case is a php header refresh with offset, see bellow:
$offset = isset( $_GET['offset'] ) ? $_GET['offset'] : 0;
$mysender2= "";
$myrecepient2= "923336088811,923126812536,923134126153";
$mymessage2 = "this is message";
$api_key = "****";
$api_secret="****";
$to_arr = explode( ",", $myrecepient2 );
if( $offset > count( $to_arr ) )
{
echo 'Completed!';
}
else
{
$b = $to_arr[ $offset ];
$url = "https://rest.nexmo.com/sms/json?" .
"api_key=".$api_key. "&" .
"api_secret=" .$api_secret . "&" .
"from=" . urlencode($mysender2) . "&" .
"to=" . urlencode($b) . "&" .
"text=" .urlencode($mymessage2);
$c = curl_init($url);
// Use SSL
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
$html = curl_exec($c);
curl_close ($c);
header('Refresh: 0.5; url=YOUR-URL?offset=' . $offset + 1 );
// half a second
}
Hey I'm sure this is an easy fix but it's driving me nuts.
I'm working with the youtube api and I'm trying to post a user generated search term into the url like so:
<form action="pagination.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>
<?
$search_term = $_POST['search_term'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds
/api/videos?q='.$search_term.'&safeSearch=none&orderby=viewCount&v=2&alt=json&start-
index=75&max-results=50');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$data = json_decode($output,true);
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);
echo "<ul style='float:right'>";
foreach($video as $video) {
echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url'].'"><br><br>';
$title = $video['title']['$t'];
$video_id = $video['media$group']['yt$videoid']['$t'];
echo ''.$title.'';
echo '<br>';
When I run this code nothing happens, however if I manually assign a value to $search_term like this:
$search_term = 'baseball';
everything works perfectly.
Any help would be greatly appreciated!
I found some syntax errors.
The following code works for me.
NOTE: The change of the filename so you can test this file and update the code in your own file.
Maybe on purpose, but at start it will always do a retrieve with "no terms", since the code is executed.
ENTER terms and press Submit button
<form action="ytcurltest1.php" method="post">
<input style="width:50%" type="text" name="search_term">
<input type="submit" value="Submit">
</form>
<?PHP
$search_term = $_POST['search_term'];
$startIndex = 1;
$maxResults = 25;
$ch = curl_init();
$url = 'http://gdata.youtube.com/feeds/api/videos?q='
. $search_term
. '&safeSearch=none&orderby=viewCount&v=2&alt=json'
. '&start-index=' . $startIndex
. '&max-results=' . $maxResults;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$data = json_decode($output,true);
if ( count($data) == 0 )
{
echo 'NO RESULTS FOUND. ENTER other terms';
RETURN;
}
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);
echo "<ul style='float:right'>";
foreach ($video as $video)
{
echo '<img src="'.$video['media$group']['media$thumbnail'][0]['url']
.'"><br><br>';
$title = $video['title']['$t'];
$video_id = $video['media$group']['yt$videoid']['$t'];
echo ''.$title.'';
echo '<br>';
}
?>