dongping1689 2014-06-05 08:21
浏览 54

在php中使用ajax移动选定的图像

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,

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)