简体   繁体   中英

Passing form value as POST and getting JSON as response

Hello guys here is my form setup:

<form class="form" id="getstat" name="getstat" method="post" action="stat101.php" />
<pre>Enter the Message ID and hit the submit button:</pre>
<!-- <input type="text" name="msgid" /> -->
<textarea name="msgids" id="msgid" wrap="physical" cols="40" rows="10"></textarea><br 
/>
<input type="submit" name="submit" value="Check Status">
</form>

My stat101.php looks like:

$user="101";
$pass="xxx";
extract($_POST);
$ids=$_POST['msgid'];
$url="http://url.com";

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: 
application/json'));
curl_setopt($c, CURLOPT_URL, "$url".$user.'/'.$pass.'
/VALUEOFIDS');
$content = curl_exec($c);
curl_close($c);
print_r($content);

The above code works if I hard code the "VALUEOFIDS", but I do not know how to get it work by posting from the form's input "msgid". The URL with the json formatted response I need to emulate is: http://url.com/ {key}/{secret}/{msgid}. I'm having a hard time getting the correct code.

What I need to emulate is: curl -H "Accept: application/json" https://api.url.com/server/search/ $1/$2/$3

When I do it in bash, it works fine.

Thank you in advance!

Inputs and textareas pass their values to the $_POST array with their name as the key. Your code example is using the id and not the name to fetch the value from the request. Try using the following:

// trim whitespace and carriage returns made possible by the textarea.
$ids = trim($_POST['msgids']); 

...

curl_setopt($c, CURLOPT_URL, "$url".$user.'/'.$pass.'/'.$ids);

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