【问题标题】:Passing form value as POST and getting JSON as response将表单值作为 POST 传递并获取 JSON 作为响应
【发布时间】:2012-05-19 04:15:38
【问题描述】:

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

<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 中执行此操作时,它工作正常。

提前谢谢你!

【问题讨论】:

    标签: php json rest


    【解决方案1】:

    输入和文本区域将它们的值传递给 $_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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 2018-09-14
      • 2016-07-11
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多