I am using an API (php) provided by Instagram to import user's images to my webpage. It is working perfectly. But I want to add one functionality to it of moving selected images to user's folder (or anywhere). I thought it doing the post method but as the OAuth code is working only once this will not help. So I decided to do it with ajax... but still no success... Please help me solve this. I am pasting my index.php and success.php below.
index.php
<?php
require 'instagram.class.php';
// initialize class
$instagram = new Instagram(array(
'apiKey' => 'YOUR_API_KEY',
'apiSecret' => 'YOUR_API_SECRET',
'apiCallback' => 'http://localhost/MyApi/Instagram/success.php' // must point to success.php
));
// create login URL
$loginUrl = $instagram->getLoginUrl();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instagram - OAuth Login</title>
<link rel="stylesheet" type="text/css" href="assets/style.css">
<style>
.login {
display: block;
font-size: 20px;
font-weight: bold;
margin-top: 50px;
}
</style>
</head>
<body>
<div class="container">
<header class="clearfix">
<h1>Instagram <span>display your photo stream</span></h1>
</header>
<div class="main">
<ul class="grid">
<li><img src="assets/instagram-big.png" alt="Instagram logo"></li>
<li>
<a class="login" href="<? echo $loginUrl ?>">» Login with Instagram</a>
<h>Use your Instagram account to login.</h4>
</li>
</ul>
</div>
</div>
</body>
</html>
success.php
<?php
ini_set("display_errors",1);
/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 01.10.2013
*/
require_once 'instagram.class.php';
// initialize class
$instagram = new Instagram(array(
'apiKey' => 'YOUR_API_KEY',
'apiSecret' => 'YOUR_API_SECRET',
'apiCallback' => 'http://localhost/MyApi/Instagram/success.php' // must point to success.php
));
// receive OAuth code parameter
$code = $_GET['code'];
// check whether the user has granted access
if (isset($code)) {
// receive OAuth token object
$data = $instagram->getOAuthToken($code);
$username = $username = $data->user->username;
// store user access token
$instagram->setAccessToken($data);
// now you have access to all authenticated user methods
$result = $instagram->getUserMedia();
} else {
// check whether an error occurred
if (isset($_GET['error'])) {
echo 'An error occurred: ' . $_GET['error_description'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instagram - photo stream</title>
<link href="https://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet">
<link href="assets/style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<header class="clearfix">
<img src="assets/instagram.png" alt="Instagram logo">
<h1>Instagram photos <span>taken by <? echo $data->user->username ?></span></h1>
</header>
<form method="POST" action="">
<table>
<tr>
<?php
$i=0;
// display all user likes
foreach ($result->data as $media) {
if($i==0 || $i%5 == 0)
{
echo "</tr><tr>";
}
// output images
if ($media->type === 'image') {
// image
$image = $media->images->low_resolution->url;
?>
<td align="center"><input type="checkbox" id="instagram_<?=$i;?>" name="instagram[]" value="<?php echo $image ?>"></td>
<td><img src="<?php echo $image ?>" width="200px" height="200px"/></td>
<?php
}
$i++;
}
?>
</tr>
</table>
<input type="submit" name="copy" id="copy" value="Copy Selected Files"><!-- onClick="copyImage('instagram[]', '1')" > -->
</form>
</div>
</body>
</html>
<!-- <script type="text/javascript">
$("#copy").click(function(e) {
e.preventDefault();
var name = $("#name").val();
var last_name = $("#last_name").val();
var dataString = 'name='+name+'&last_name='+last_name;
$.ajax({
type:'POST',
data:dataString,
url:'success.php',
success:function(data) {
alert(data);
}
});
});
</script> -->
<?php
if(isset($_POST['copy']))
{
/*$content = file_get_contents("https://scontent-b.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10341463_1428435114089500_7065440848492935201_n.jpg ");
//Store in the filesystem.
$fp = fopen("/var/www/tmpImages/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);*/
$imgArray = $_POST['instagram'];
$i=1;
foreach ($imgArray as $img)
{
$content = file_get_contents($img);
//Store in the filesystem.
$toPath = "/var/www/tmpImages/image".$i.".jpg";
$fp = fopen($toPath, "w");
fwrite($fp, $content);
fclose($fp);
$i++;
}
}
?>
Please help me out with this... Thank you,