将表单值作为 POST 传递并获取 JSON 作为响应


Passing form value as POST and getting JSON as response

大家好,这是我的表单设置:

<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>

我的 stat101.php 看起来像:

$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);

如果我对"VALUEOFID"进行硬编码,上面的代码可以工作,但我不知道如何通过从表单的输入"msgid"发布来使其工作。我需要模拟的 json 格式响应的 URL 是:http://url.com/{key}/{secret}/{msgid}。我很难获得正确的代码。

我需要模拟的是:curl -H "Accept: application/json" https://api.url.com/server/search/$1/$2/$3

当我在 bash 中执行此操作时,它工作正常。

提前谢谢你!

输入和文本区域将其值传递给 $_POST 数组,并将它们的名称作为键。您的代码示例使用 id 而不是名称从请求中获取值。尝试使用以下方法:

// trim whitespace and carriage returns made possible by the textarea.
$ids = trim($_POST['msgids']); 
...
curl_setopt($c, CURLOPT_URL, "$url".$user.'/'.$pass.'/'.$ids);