簡體   English   中英

將表單值作為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);

如果我對“ VALUEOFIDS”進行硬編碼,則上面的代碼可以工作,但是我不知道如何通過從表單輸入“ 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM