简体   繁体   中英

Get contents of form and then add on to url with php

I have found a stack overflow answer that shows me how to get the number of followers from Instagram. Basically, it adds the username onto the URL. I want to be able to get that username from an input box and then add it to the url.

    $raw = file_get_contents('https://www.instagram.com/USERNAME'); //replace with user
preg_match('/\"edge_followed_by\"\:\s?\{\"count\"\:\s?([0-9]+)/',$raw,$m);
print intval($m[1]);

Right now I'm thinking of replacing the username with $_GET and connecting that to my form. How do I pull off this? Thanks.

Do this: in file.php

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){
$username = $_POST['username'];
//check if length of username is greater 3 
if (strlen($username) < 3)
    echo "Kindly enter a valid username";
else{
    $url = "https://www.instagram.com/".$username;
    $raw =  file_get_contents($url, false, null, 0, 100);
}
}

?>

In Your HTML file

<form method="POST" action='file.php'>
<label for='username'>Enter Username</label>
<input type='text' required name='username' id='username' />
<input type='submit' value='Submit' name='submit' />

you may try this code. I encode all the result via json from instagram. method: 1. Get username from html 2. perform a json decode on file get contents 3 output the result and try on your browser.

if(isset($_POST['submit'])){
$username = $_POST['username'];
$url = 'https://www.instagram.com/'.$username;
$jsonData = json_encode(file_get_contents($url));
echo $jsonData;
// you may extend your code from here to achieve what you wanted
}

Hope this code might help you.

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