简体   繁体   English

API调用的HTML表单PHP脚本操作

[英]HTML Form PHP script action for API call

I'm to use a submission form to my API call a little more user friendly. 我将使用提交表单向我的API调用更加用户友好。 I wish to do this via a simple HTML form which is as such: 我希望通过一个简单的HTML表单来做到这一点:

input type="text" name="ID:" placeholder="ID">

Upon hitting the submit button it will call my PHP file which makes the API call. 点击提交按钮后,它将调用我的PHP文件,该文件将进行API调用。

Normally I would just amend my PHP Script to suit the information i'm calling but as stated want to make this easier for a user to submit. 通常,我只是修改PHP脚本以适合我要调用的信息,但如上所述,这是为了使用户更易于提交。 Here is a snippit from the PHP: 这是PHP的代码段:

<?php
$id = "123456";
$apiurl = "http://url.co.uk/api/information.json?id=" . $id;

Instead of me manually entering "123456" on the php how can I use the form to submit the data. 代替我在php上手动输入“ 123456”,我该如何使用表单提交数据。

I tried adding $ID = $_POST["ID"]; 我尝试添加$ID = $_POST["ID"];

But this only fails for me. 但这仅对我失败。

Theres a typo in your input. 您输入的内容有错别字。 Instead of name " ID: " you should have just ID, thats why it doesnt work - because theres no element with index 'ID' in POST array. 而不是名称“ ID:”,您应该只具有ID,这就是为什么它不起作用的原因-因为POST数组中没有索引为“ ID”的元素。

So simply: input type="text" name="ID" placeholder="ID" /> 如此简单:输入type =“ text” name =“ ID” placeholder =“ ID” />

And then , in PHP 然后,在PHP中

<?php
$id = $POST['ID'];
$apiurl = "http://url.co.uk/api/information.json?id=" . $id;
?>

Either change the $POST['ID'] to $POST['ID:'] or change the name of the input 将$ POST ['ID']更改为$ POST ['ID:']或更改输入名称

Remember to specify the method of posting your form to the php file. 记住要指定将表单发布到php文件的方法。

http://www.w3schools.com/html/html_forms.asp http://www.w3schools.com/html/html_forms.asp

instead of $ID = $_POST["ID"]; 而不是$ ID = $ _POST [“ ID”]; use $ID = $_POST["ID:"]; 使用$ ID = $ _POST [“ ID:”];

Check out PHP's documentation about curl. 查看有关curl的PHP 文档 It might help you. 它可能会帮助您。

<?php 
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM