I get 'json is null' as error
My php file:
<?php
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$url='https://www.googleapis.com/urlshortener/v1/';
$key='ApiKey';
$result= $url.($query).$key;
$ch = curl_init($result);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
}
?>
My html:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// when the user clicks the button
$("button").click(function(){
$.getJSON("shortner.php?query="+$('#query').attr("value"),function(json){
$('#results').append('<p>Id : ' + json.id+ '</p>');
$('#results').append('<p>Longurl: ' + json.longurl+ '</p>');
});
});
});
</script>
</head>
<body>
<input type="text" value="Enter a place" id="query" /><button>Get Coordinates</button>
<div id="results"></div>
Edited :
<?php
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$url='https://www.googleapis.com/urlshortener/v1/';
$key='Api';
$key2='?key=';
$result= $url.$query.$key2.$key;
$requestData= json_encode($result);
echo var_dump($query);
$ch = curl_init($requestData);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
}
?>
Worked for me:
<?php
$ch = curl_init("https://www.googleapis.com/urlshortener/v1/url?key=MYAPIKEY&shortUrl=http://goo.gl/fbsS");
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$res = curl_exec($ch);
if (!$res)
var_dump(curl_error($ch)); else
echo "Yay! I've got JSON result!";
curl_close($ch);
?>
Still there is bit.ly service with similar but more powerful functions. Here is the example on how to use it (works just perfectly):
<?php
if (array_key_exists("url", $_GET) && $_GET["url"] != "")
{
$u_name = "YOUR BIT.LY USERNAME";
$u_apikey = "YOUR BIT.LY API KEY";
$f = fopen("http://api.bit.ly/v3/shorten?login=" . $u_name . "&apiKey=" . $u_apikey . "&longUrl=" . rawurlencode($_GET["url"]) . "&format=json", "r");
header("Content-Type: application/json");
ob_start();
echo stream_get_contents($f);
fclose($f);
ob_end_flush();
exit;
}
?>
<html>
<head>
<title>jCarousel Examples</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("[name=send]").click(function() {
$.getJSON("index.php?url=" + $('[name=url]').val(), function(f) {
$('[name=res]').append('<p>Shorten: ' + f.data.url + '</p><br/ >');
});
});
});
</script>
</head>
<body>
<input type="text" name="url" size="30" />
<input type="button" name="send" value="shorten!" />
<br /><div name="res"></div>
</body>
</html>
UPD: To get your bit.ly API Key, register on the bit.ly website and go to your account (in the top) -> Settings.
Related
i have an script and its work without problems on local host.
here is my code ...
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
if(isset($_POST['username'])){
$username=$_POST['username'];
//echo $username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.instagram.com/$username/?__a=1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
// curl_setopt($ch, CURLOPT_FAILONERROR,true);
// curl_setopt($ch, CURLOPT_ENCODING,"");
// curl_setopt($ch, CURLOPT_VERBOSE, true);
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
curl_close($ch);
$json=json_decode($result);
//var_dump($json);
$insta_profile_picture= $json->graphql->user->profile_pic_url_hd;
}
else{
}
?>
<form method="post" action="">
<input type="text" name="username" placeholder="Type Your Username of Instagram " style="width:300px;height: 30px;border-radius: 4px;">
<input type="submit" name="submit" style="width:300px;height: 35px;border-radius: 4px;">
</form>
<?php
if(isset($_POST['username'])){
if($insta_profile_picture){
//echo $insta_profile_picture;
$imageData = base64_encode(file_get_contents($insta_profile_picture));
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';}
else{
echo "something wrong";
}
}
else{
echo "please select your username on top input for checking profile photo";
}
?>
</body>
</html>
but it not work on my server and with tracking the result i understand its redirect to https://www.instagram.com/accounts/login/
please help me
tnx a lot for your helping
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 am a complete newbie to programming and am working on a sample project. I am trying to send an AJAX request to Apple to get a video from a user-defined artist. However, I don't get any data returned to my function and nothing shows up in the console.
Index.php
<!DOCTYPE html>
<html>
<head>
<title>iTunes Video Search</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function() {
$.post($(this).attr('action'), $(this).serialize(), function(res) {
var htmlStr = '';
if (res.results.length !== 0) {
htmlStr += "<video controls src='" + res.results[0].previewUrl + "'></video>";
} else {
htmlStr += "Not Found";
}
$('#video').html(htmlStr);
}, 'json');
return false;
});
});
</script>
</head>
<body>
<h2>Enter Artist's Name</h2>
<form action="/videos/create" method="post">
<input type="search" name="artist_name" id="artist_name">
<input type="submit" value="Search">
</form>
<div id="video"></div>
</body>
</html>
Videos.php
<?php
class Videos extends CI_Controller {
public function index() {
$this->load->view('index');
}
public function create() {
$artist_name = str_replace(' ', '', $this->input->post('artist_name'));
$url = "https://itunes.apple.com/search?term=" . $artist_name . "&entity=musicVideo";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $data;
}
}
?>
Any help is greatly appreciated!
this is my index
<body>
<form id="form1" method="post" name="entrar" action="procesar.php">
<p>Usuario
<label for="username">:</label>
<input type="text" name="username" id="username">
</p>
<p>
<label for="pass">Clave:</label>
<input type="password" name="pass" id="pass">
</p>
<p>
<input type="submit" name="entrar" id="button" value="Enviar">
</p>
</form>
</body>
this is de code of procesa.php when check the values username and pass
<?php
if($_POST['entrar']){
$user = $_POST['username'];
$pass = $_POST['pass'];
if($user == 'gato' && $pass == 'gato'){
setcookie('control','1234',time()+30000,'/','www.onlinelatino.org');
header("Location: sitio.php");
}
else{
header("Location: error.php");
}
}
else{
echo "error inclucion no valida";
}
?>
later go to here
<!doctype html>
<html>
<head><title>Welcome</title></head>
<body>
<?php
$control = $_COOKIE['control'];
if($control == 1234){
?>
<h1>Hola mundo</h1>
<h2>Como estas</h2>
<?php
}
else{
echo "No estas logado";
}
?>
</body>
</html>
this work fine but i wanna access to site when a robots this is a code
<?php
$parametros_post = 'username='.urlencode("gato").'&pass='.urlencode("gato").'&entrar'.urlencode("Enviar");
$cookie_file = "cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.onlinelatino.org/curl/procesar.php");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01 Windows NT 5.0)");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: es-es, en"));
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parametros_post);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
echo $result;
echo $error;
?>
but no start the sesion with the cookie
only say say this "error inclucion no valida" this are on procesar.php
the link of the example is this
http://www.onlinelatino.org/curl/index.html
and the robot is this
http://www.onlinelatino.org/curl/bot.php
I don't have idea when are the error
'&entrar'.urlencode("Enviar")
You're missing an =
'&entrar='.urlencode("Enviar")
I have the following code, but it doesn't update my status (the username and password has been removed for this snippet) and it throws the error eeek. Any ideas why it's not working?
EDIT:
Apparently Twitter NEEDS OAuth instead now. I have been following this tutorial here: http://ditio.net/2010/06/07/twitter-php-oauth-update-status/
I have registered my App and copied both the Library and Tokens over.
BUT I'm not sure how to get this working with my current code, ie. what needs changing/adding/removing.
My Updated code:
<?php
function postToTwitter($username,$password,$message)
{
require_once 'TwitterOAuth.php';
define("CONSUMER_KEY", "???");
define("CONSUMER_SECRET", "???");
define("OAUTH_TOKEN", "???");
define("OAUTH_SECRET", "???");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => date(DATE_RFC822)));
// GET the API url via web authentication
// add 'status' param to send a message to Twitter
$host = "http://api.twitter.com/1/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
// Go for it!!!
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
// close curl
curl_close($ch);
//echo "http code: ".$resultArray['http_code']."<br />";
if($resultArray['http_code'] == "200"){
echo "<br />OK! posted to http://twitter.com/".$username."/<br />";
} else {
echo "eek! yegads! error posting to Twitter";
}
// debug the result
// echo "<pre>";
// print_r($resultArray);
// echo "</pre><hr>";
// $sResult = htmlentities($result);
// $sResult = str_replace("><","><br /><",$sResult);
// echo "<pre>";
// print $sResult;
// echo "</pre>";
}
if(isset($_POST['submit']))
{
$textmessage = $_POST['message'];
postToTwitter("whiningwall","u0558234",$textmessage);
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=1024">
<title></title>
<link rel="stylesheet" type="text/css" href="master.css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<form id="post" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<label for="textArea">What's on your mind?</label>
<textarea id="textArea" name="message" maxlength="140"></textarea>
<div class="submit">
<span class="charsRemaining"></span>
<input type="submit" name="submit" value="Share" />
</div>
</fieldset>
</form>
<script type="text/javascript" src="jquery.maxlength.js"></script>
<script type="text/javascript">
$('#textArea').focus();
</script>
</body>
</html>
I believe that CURLOPT_USERPWD is for Apache's basic authentication, which Twitter does not use. I don't know much about the Twitter API but I think you'll be better off using OAuth to tell Twitter who you are.
Edit to add: There's GPL twitter library at http://code.google.com/p/php-twitter/ that you might try looking at (or even using).