Code for /www/formulair.php :
<html>
<head>
</head>
<body>
<form action="recive.php" method="POST">
<input type="text" name="name"/>
<input type="submit" value="Send me your name!">
</form>
</body>
</html>
Code for /www/recive.php :
<?php
include ("config.php");
?>
Code for /www/config.php :
<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://localhost/test.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $_POST['name']);
curl_exec($ch);
curl_close($ch);
?>
Code for test.php :
<?php
if(isset($_POST['name'])) {
$fp = fopen('data.txt', 'a+');
fwrite($fp,$_POST['name']);
fclose($fp);
}
?>
Error :
Erreur : Notice: Undefined variable
What I should write in test.php to get the $_POST['name']?
Related
I have added this basic login page to server for testing curl form submitting:
<?php
if(isset($_POST['submit'])){
$email = $_POST['email'];
$password = $_POST['password'];
if($email == "email#gmail.com"){
if($password == "123456"){
echo "Correctamundo!";
}else{
echo "Wrong pass";
}
}else{
echo "Wrong Email";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form method="POST" action="">
<input type="email" name="email" value="">
<input type="password" name="password" value="">
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
So if the email equals to email#gmail.com && the password was 123456, the Correctamundo message appears on the page.
Now with curl at another file I've added this:
<?php
$data = array(
"email" => "email#gmail.com",
"password" => "123456",
"form" => "submit"
);
$ch = curl_init("http://sitename.com/login.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Allow Redirections
curl_setopt($ch, CURLOPT_POST, true); // Making a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // COOKIE-jar: To save data for cookie created for login process
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE means Don't just echo output the data instead we can store the Request response in some variable
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Now as you can see I have passed the correct credentials as $data array. But when I load the page, I don't get the Correctamundo message as success message.
So why is that? What am I doing wrong here?
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 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 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.