简体   繁体   中英

Javascript Variable to Php json decoded

Actually i've used this script to get the profile picture of instagram user

<script src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
  <script type="text/javascript">
var user_id = <? echo $page_id; ?>;
var url = "https://api.instagram.com/v1/users/"+user_id+"?client_id=775829274b6f48c1ac2bf218dda60e16&callback=?";
$.getJSON(url, function(data) {
    $("body").append("<img src='"+data.data.profile_picture+"' />");

}
);

  </script>

The Output of this script is 100% what i need. All i need now is to take the image link to a PHP variable which is (+data.data.profile_picture+)

if tried to reload the same page with + ?link="+data.data.profile_picture+" so i can grab it with $_GET['link'] Like this:

window.location.href = location.href + '?link="+data.data.profile_picture+";

but it doesn't .. What should i do to pass this javascript variable to a PHP variable.

尝试这个

window.location.href = location.href + '?link='+data.data.profile_picture;

You need to send your data to the PHP, and the simplest way would be using jQuery.ajax() .

var picture = data.data.profile_picture;
$("body").append("<img src='"+picture+"' />");
$.ajax({
            url: "http://example.com/webservices/getProfilePicture.php",
            type: "POST",
            data: {
                'profile_picture': picture
            },
            success: function () {
                console.log('Success!');
                // Place eventual post-treatment here
            },
            error: function () {
                 console.log('Error..');
                 // Place eventual error-handling / debugging here
            }
        }
);

});

Then in PHP you can access it with this:

<?php $profile_picture = $_REQUEST['profile_picture']; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM