I am trying to search and get youtube videos on my webpage. but have some error want your help what to do know.
HTML code is:
<h2>Search Videos by keyword using YouTube Data API V3</h2>
<div class="search-form-container">
<form id="keywordForm" method="post" action="">
<div class="input-row">
Search Keyword : <input class="input-field" type="search"
id="keyword" name="keyword"
placeholder="Enter Search Keyword">
</div>
<input class="btn-submit" type="submit" name="submit"
value="Search">
</form>
</div>
Now moving to the PHP code where I used iframe for the video title and use my own created API from the developer account. But when I run the code I get an error about MAX_RESULT.
and the PHP code and youtube API is here:
<?php
if (isset($_POST['submit']) )
{
$keyword = $_POST['keyword'];
if (empty($keyword))
{
$response = array(
"type" => "error",
"message" => "Please enter the keyword."
);
}
}
?>
<?php
if(!empty($response)) {
?>
<div class="response <?php echo $response["type"]; ?>
">
<?php echo $response["message"]; ?>
</div>
<?php
}
?>
<?php
if (isset($_POST['submit']) )
{
if (!empty($keyword))
{
$apikey = 'AIzaSyAjowKxG2mRdr7p1N-3RtBd-BWAgjbf87g';
$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++) {
$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
}
}
}
?>
Edit:
Error message (copied from the comments):
Notice: Use of undefined constant MAX_RESULTS - assumed 'MAX_RESULTS' in C:\xampp\htdocs\php\youtube\youtubeVideoAPI.php on line 48 . On my text editor line, 48 code is $googleApiUrl = 'googleapis.com/youtube/v3/search?part=snippet&q=' . $keyword . '&maxResults=' . MAX_RESULTS . '&key=' . $apikey;
Check, if you have defined the MAX_RESULTS constant in your code.
If you want to define a constant, you can do it this way:
define('MAX_RESULTS', 1); // I don't know, what value you need, so you can the 1 for anything you want
Related
I'm making an auto SMS system. But SMS provider gave me post method form but I want it to modify with curl command by my own.
they provided me
<head>
<title>SMS</title>
</head>
<body>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" /><br />
<input type="textarea" placeholder="sms" name="msg" /><br />
<input type="submit" value=" OK " />
</form>
<?php
$apikey='$2y$10$t..Yr.YDG0kXYiuLwQ78OecDgz6/qh.1xSWx77mXjczkk3AEKvTZe';
if (isset($_POST["msg"]) ) {
$sendto = $_POST["to"];
$fullNumber = '880' . substr(preg_replace('/\D/', '', $sendto), -10);
$msg = urlencode($_POST["msg"]);
// $masking='CITY PORTER';
// $masking=urlencode($masking);
// masking $url='http://example.com/smsapi/masking?api_key='.$apikey.'&smsType=text&maskingID='.$masking.'&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
$url='http://example.com/smsapi/non-masking?api_key='.$apikey.'&smsType=text&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
if ( !empty($_POST["to"]) && !empty($_POST["msg"])) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL =>$url,
CURLOPT_USERAGENT =>'My Browser'
));
$resp = curl_exec($curl);
echo $resp;
curl_close($curl);
}
else
{ echo "Field is empty";}
}
?>
</body>
I converted it to curl command but this is not still working. I just want to input multiple value without is set() 1 value.
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row[mailing_no];
$message = "Dear Guardian, ". $row[name] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);
Try this i hope your problem will be solve
<div id="send_reply"></div>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" id="phone" /><br />
<input type="textarea" placeholder="sms" name="msg" id="mess" /><br />
<input type="submit" value=" OK " id="send_btn" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#send_btn").click(function(){
var txt1=$("#phone").val();
var txt2=$("#mess").val();
$.ajax({
url : 'check.php',
method : 'GET',
data : {txt1:txt1,txt2:txt2},
success:function(dda){
$("#send_reply").html(dda);
}
});
});
});
</script>
I think we were missing double quotes for $row keys, try this:
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row["mailing_no"];
$message = "Dear Guardian, ". $row["name"] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);
I have a class in php that sends push notification to android/IOS users of the app through web. Now I need to add emoji's to title/body of the push notification payload, but no luck doing so.
I've already tried convert encoding to UTF-8, I've tried using this library and I've tried replacing \\ to \ (json_encode function adds \\ instead of \ on e.g \xC2\xA9 becomes \\xC2\\xA9).
Here's my code :
public function sendPush($payload)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->base_url . "/push");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-AUTH-TOKEN: 94h4f5RCDSEYKYqXDHTk';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
That's push function, and here's the .tpl where I get the data from web.
<?php
include(SITE_ROOT . '/core/admin/logo.class.php');
if (isset($_POST['push-button'])) {
if (
isset($_POST['push-title'])
&& isset($_POST['push-body'])
&& isset($_POST['push-city'])
&& isset($_POST['push-url'])
&& isset($_POST['push-platform'])
) {
$push = new Push();
$title = $_POST['push-title'];
$body = $_POST['push-body'];
$url = $_POST['push-url'];
$platform = $_POST['push-platform'];
$cities = $_POST['push-city'];
$payload = [
'title' => $title,
'body' => $body,
'url' => $url,
'cities' => [
$cities
]
];
if ($platform === 'all') {
$responses_ios = json_decode($push->sendPush($payload), true);
$responses_android = json_decode($push->sendPushAndroid($payload), true);
} elseif ($platform === 'ios') {
$responses_ios = json_decode($push->sendPush($payload), true);
} elseif ($platform === 'android') {
$responses_android = json_decode($push->sendPushAndroid($payload), true);
}
// $devices = '';
// var_dump($responses);
// foreach ($responses_ios as $response) {
// $devices .= $response . "<br>";
// }
$message = '<span class="message"><strong>PUSH-notiser har skickats</strong></span>';
unset($_SESSION['message']);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>PUSH - Administration</title>
<?php include(SITE_ROOT . '/views/admin/meta.tpl'); ?>
</head>
<body class="page-admin">
<?php include(SITE_ROOT . '/views/admin/header.tpl'); ?>
<?php include(SITE_ROOT . '/views/admin/navigation.tpl'); ?>
<p class="shoutout p-45 text-center">Skicka PUSH-meddelanden till användare i en stad</p>
<?php echo $message; ?>
<section class="wrapper">
<section class="container centered">
<section class="col-3-5">
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<section class="row">
<span class="input-title">Välj stad</span>
<span class="input-field">
<select name="push-city">
<option value="all">Alla</option>
<?php
$cities = $app->get_page_list('city');
foreach ($cities as $city) {
echo '<option value=' . $city['page_name'] . ' />' . $city['page_name'] . '</option>';
}
?>
</select>
</span>
</section>
<section class="row">
<span class="input-title">Titel</span>
<span class="input-field">
<input type="text" name="push-title" class="slug" placeholder="Titel på meddelande"
required/>
</span>
</section>
<section class="row">
<span class="input-title">Meddelande</span>
<span class="input-field">
<textarea name="push-body" class="slug" placeholder="Meddelande" required></textarea>
</span>
</section>
<section class="row">
<span class="input-title">URL</span>
<span class="input-field">
<input type="text" name="push-url" class="slug" placeholder="URL att skicka med" required/>
</span>
</section>
<section class="row">
<span class="input-title">Plattform</span>
<span class="input-field">
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="all" checked required/>Alla
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="ios" required/>iOS
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="android" required/>Android
</span>
</section>
<section class="row" style="margin-top: 2rem;">
<input type="submit" class="button-large-2 button-no-g" name="push-button"
value="Skicka PUSH-notis"/>
</section>
</form>
</section>
</section>
</section>
<?php include(SITE_ROOT . '/views/admin/emoji.tpl'); ?>
<?php include(SITE_ROOT . '/views/admin/footer.tpl'); ?>
</body>
</html>
Here's what I get from echo/var_dump on title/body
string(13) "test \xC2\xA9" string(13) "test \xC2\xA9"
Those 2 \xC2\xA9 should be copyright signs.
I am trying to retrieve user info on my webpage using the api, but it does not return any info.
Index.php:
<!DOCTYPE html>
<html>
<Head>
<title>IG LP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</Head>
<center>
<body>
<img src = "http://i.imgur.com/Q38jJG9.png">
<center>
<p>See your profile information on the next page</p>
<form role="form" method="post" action="getuser.php">
<div class="form-group">
<font color="white">Instagram name:</font>
<input type="Text" id="username" name="username" placeholder="Your Instagram Name:">
<input type="submit" value="Submit" class="btn btn-primary btn-lg btn-block">
</div>
</form>
</center>
</body>
</html>
getuser.php:
<?PHP
$username = $_POST['username'];
// Add your own access token bellow to make this script work
$accesstoken = 'My Access token';
$apiurl = 'https://api.instagram.com/v1/users/search?q='.$username.'&access_token='.$accesstoken;
//API call to grab profile pic, username, bio, website, full name, and id
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiurl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
$pic = $json{'data'}[0]{'profile_picture'};
$username = $json{'data'}[0]{'username'};
$bio = $json{'data'}[0]{'bio'};
$website = $json{'data'}[0]{'website'};
$full_name = $json{'data'}[0]{'full_name'};
$id = $json{'data'}[0]{'id'};
// API call to grab Follows, Followed By, and amount of pics upload.
// Delete this if you don't need this info.
$apiurl = 'https://api.instagram.com/v1/users/'.$id.'/?access_token='.$accesstoken;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiurl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);
curl_close($curl);
$json = json_decode($response, true);
$pics = $json{'data'}{'counts'}{'media'};
$follows = $json{'data'}{'counts'}{'follows'};
$followed_by = $json{'data'}{'counts'}{'followed_by'};
?>
<!DOCTYPE html>
<html>
<Head>
<title>IG LP</title>
</Head>
<body>
<center>
<img src = "<?php echo $pic; ?>">
<p>Username: <?php echo $username; ?></p>
<p>Bio: <?php echo $bio; ?></p>
<p>Website: <?php echo $website; ?></p>
<p>Full Name: <?php echo $full_name; ?></p>
<p>ID: <?php echo $id; ?></p>
<p>Pics Uploaded: <?php echo $pics; ?></p>
<p>Following: <?php echo $follows; ?></p>
<p>Followed By: <?php echo $followed_by; ?></p>
</center>
</body>
</html>
What can I do to get the user info inserted in the textbox?
I'm trying to search a user using the Github api, but I'm not sure what I'm doing wrong here.
My html form:
<form action="" method="get">
<div class="form-group">
<label >Buscar Usuario</label>
<input class="form-control" type="text" name="username">
</div>
<button type="submit" name="submit" class="btn btn-default">Procurar</button>
</form>
My php code:
<?php
function buscaUser()
{
$user= $_GET['username'];
$ch = curl_init();
$url ="https://api.github.com/search/users?q=".$user;
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
}
?>
YOu didn't call the buscaUser() anywhere
Do it this way:
<?php
function buscaUser($user) {
$ch = curl_init();
$url ="https://api.github.com/search/users?q=".$user;
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
}
?>
Then you can call the function and pass the submitted value:
<?php
echo buscaUser($_GET['username']);
?>
Should do!
I try to get news from website
http://www.science-support.ru/news.html
, but I can't extract anything from web page content.
When I apply file_get_contents() I get string for which other function don't work. Initially, I try to use file_get_html() from simple_html_dom.php, but then other functions from this source don't recognize any elements in obtained DOM.
About other function:
<?php
$content = file_get_contents('http://www.science-support.ru/news.html'); //normal page
$content = substr($content,20); //strange characters
$content_arr = explode( 'div id="box3"' , $content ); //doesn't work
echo $content;
echo $content_arr[0];
?>
After substr() I get something like
" <�/tr> <�/table> <�/div> <�div id="box3"><�!-- InstanceBeginEditable name="page-content" --> <�h4 class="yellow">14.11.2014 />2>AB8 $>=40/<�/h4> <�p>1JO2;O=K #57C;LB0BK :>=:C#A0 =0 ?#8AC645=85 ?>8A:>2KE 3#0=B>2 ?> #>3#0<<5 =0CG=>-B5E=8G5A:>9 <>45#=870F88 8 ?>2KH5=8N :20;8D8:0F88 <>;>4KE CG5=KE >AA88 2014-2015 33.<�a href="news/news2014-nota-res.html" class="txt" >?>4#>1=55<�/a><�/p>"
How can I extract readable content? Thanks
Try to do this from Curl i have post some code that will help you
<?php
if( isset( $_POST['site_url'] ) && !empty( $_POST['site_url'] ) ){
echo get_html($_POST['site_url']);
} else {
echo 'false';
}
function get_html($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
get website url from your html form this is html how to set
<div class="col-md-offset-2 col-md-8">
<form role="form" id="siteform" method="post">
<div class="form-group">
<input type="url" class="form-control" name="site_url" id="site_url" placeholder="Enter your site address">
<span class="help-block"></span>
</div>
<button data-loading-text="Please wait..." type="submit" id="url_getter" class="btn btn-default btn-success">Submit</button>
</form>
</div>
</div>