Problems posting data into json url - php

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>';
}
?>

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);
}

using youtubes api on a website which runs on a raspberry pi

I use the youtube api to search for videos on my own page. That works so far quite well, as long as I access the site via localhost. I use PHPStorms build-in-server (which basically is xampp) to connect via localhost to the website. But if I run the page on my Raspberry Pi and do the same search, about 90% of the videos shows the following error: "video not available".
This is the code for the api
<?php
if (isset($_POST['submit']) )
{
if (!empty($keyword))
{
$keyword = str_replace(' ', '',$keyword);
$apikey = ' '; // i removed the key
$googleApiUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $keyword . '&maxResults=' . MAX_RESULTS . '&key=' . $apikey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
?>
<div class="result-heading">About <?php echo MAX_RESULTS; ?> Results</div>
<div class="videos-data-container" id="SearchResultsDiv">
<?php
for ($i = 0; $i < MAX_RESULTS; $i++) {
if (!empty($value['items'][$i]['id']['videoId'])) {
$videoId = $value['items'][$i]['id']['videoId'];
$title = $value['items'][$i]['snippet']['title'];
$description = $value['items'][$i]['snippet']['description'];
?>
<div class="video-tile">
<div class="videoDiv">
<iframe id="iframe" style="width:100%;height:100%"
src="//www.youtube.com/embed/<?php echo $videoId; ?>"
data-autoplay-src="//www.youtube.com/embed/<?php echo $videoId; ?>?autoplay=1"></iframe>
</div>
<div class="videoInfo">
<div class="videoTitle"><b><?php echo $title; ?></b></div>
<div class="videoDesc"><?php echo $description; ?></div>
</div>
</div>
<?php
}
}
}
}
?>
</div>
does anyone know why it works on localhost but not on the raspberry pi?

there is an error when i download file with url on php

My codes work -- send variables from P.html to V.php with ajax
And there is an error when i put some code in V.php.
V.php makes span tag to P.html
When i use code that download file with url, ERROR(Uncaught TypeError: Cannot read property 'innerHTML' of null // that i put data with htmlspecialchars from V.php to P.html)
But code that download file with url And V.php code are work well when it used by dividing.
what should i do with this .
When i delete this code
curl_setopt($curlCh, CURLOPT_URL, $url);
curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlCh, CURLOPT_SSLVERSION,3);
There is no error but doesn't download file
This is an code that has error
....
$query4 = "SELECT mongoFN FROM hashDB WHERE CarID = '".$var2."' AND Day = '".$var3."'";
$result4 = mysqli_query($conn, $query4);
$row4 = mysqli_fetch_assoc($result4);
$field4 = 'http://192.168.44.111:8081/api/file/download?filename='.$var2.$row4['mongoFN'].'.h264.mp4';
?>
<?php echo "<span id='a1' style='display: none;'>".htmlspecialchars($field1)."</span>";?>
<?php echo "<span id='a2' style='display: none;'>".htmlspecialchars($field2)."</span>";?>
<?php echo "<span id='a3' style='display: none;'>".htmlspecialchars($field3)."</span>";?>
<?php echo "<span id='a4' style='display: none;'>".htmlspecialchars($field4)."</span>";?>
//New code
<?php
$url = 'http://192.168.44.111:8081/api/file/download?filename=41b775820181005182638.h264.mp4';
$curlCh = curl_init();
curl_setopt($curlCh, CURLOPT_URL, $url);
curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlCh, CURLOPT_SSLVERSION,3);
$curlData = curl_exec ($curlCh);
curl_close ($curlCh);
$downloadPath = "/Users/mac_pc/Downloads/".$field4.".mp4";
$file = fopen($downloadPath, "w+");
fputs($file, $curlData);
fclose($file);
?>
And These are works well
1.A.php (consist of only this code)
<?php
$url = 'http://192.168.44.111:8081/api/file/download?filename=41b775820181005182638.h264.mp4';
$curlCh = curl_init();
curl_setopt($curlCh, CURLOPT_URL, $url);
curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlCh, CURLOPT_SSLVERSION,3);
$curlData = curl_exec ($curlCh);
curl_close ($curlCh);
$downloadPath = "/Users/mac_pc/Downloads/"mp.mp4";
$file = fopen($downloadPath, "w+");
fputs($file, $curlData);
fclose($file);
?>
V.php ( No download file with url code)
....
$query4 = "SELECT mongoFN FROM hashDB WHERE CarID = '".$var2."' AND Day = '".$var3."'";
$result4 = mysqli_query($conn, $query4);
$row4 = mysqli_fetch_assoc($result4);
$field4 = 'http://192.168.44.111:8081/api/file/download?filename='.$var2.$row4['mongoFN'].'.h264.mp4';
?>
<?php echo "<span id='a1' style='display: none;'>".htmlspecialchars($field1)."</span>";?>
<?php echo "<span id='a2' style='display: none;'>".htmlspecialchars($field2)."</span>";?>
<?php echo "<span id='a3' style='display: none;'>".htmlspecialchars($field3)."</span>";?>
<?php echo "<span id='a4' style='display: none;'>".htmlspecialchars($field4)."</span>";?>
Because of a time delay . Sorry for it

Getting more than 10 results Google Search API

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>';
}
}
}
?>

output XML from Yahoo BOSS API with PHP

I managed to get this code so far:
<?php
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
foreach ($results->resultset_news->result as $theresult) {
echo ''.$theresult->title.'<br/>';
echo $theresult->abstract.'<br/>';
echo '<small><i>'.$theresult->dispurl.'</i></small><br/>';
echo '<br/><br/>';
}
So how exactly do i output actual XML rather than HTML?
Tried this:
echo $results->asXML();
?

Categories