[英]How to send HTTP GET request to REST API via cURL using PHP and call method from REST API with cURL
[英]I need to send API request using get variables via HTTP requests using PHP
我需要使用PHP通过HTTP请求使用get变量发送API请求。
格式类似于:
https:"www.thirdpartyurl.com"/Request.ashx?command=_&username= _&password=______&customer=__&customerpassword=___
其中1.命令=创建客户
用户名=我的用户名
密码=我的帐户密码
customer =客户用户名(他填写表格)
客户密码=客户密码(在表格中填写)
请在下面查看我的HTML文件:
<html>
<head>
<meta charset="utf-8">
<title>Sample request</title>
</head>
<body>
<form action="operations.php" method="post">
<p>userid
<input id='textbox1' type="text" name="custuname" value="" placeholder="Enter User name">
</p>
<p>password
<input id='textbox1' type="text" name="custpwd" value="" placeholder="Enter User name">
</p>
<input type="submit" name="Create Account" id="create_account" value="Create">
</form>
</body>
</html>
我的php文件是:
<?php
$baseurl = "www.thirdpartyurl.com";
$username = "xxxxx";
$pw = "xxxx";
$customer = "$_POST(custuname)";
$customerpw = "$_POST(custpwd);
&tariffrate=4;
$params = array("command"=>"createcustomer", "username"=>$username, "password"=>$pw,
"customer"=>$customer,"customerpassword"=>$customerpw,"tariffrate"=>&tariffrate);
$link = $baseurl . '?' . http_build_query($params);
?>
当单击提交按钮而不是创建帐户时,它在浏览器中的PHP代码上方显示给我。 请帮助我。
应该
$customer = $_POST['custuname'];
$customerpw = $_POST['custpwd'];
代替
$customer = "$_POST(custuname)";
$customerpw = "$_POST(custpwd); //<----quotes not closed
您能尝试一下吗, &tariffrate=4;
应该是$tariffrate=4;
添加了curl
以发送api请求
HTML:
<html>
<head>
<meta charset="utf-8">
<title>Sample request</title>
</head>
<body>
<form action="operations.php" method="post">
<p>userid
<input id='textbox1' type="text" name="custuname" value="" placeholder="Enter User name">
</p>
<p>password
<input id='textbox1' type="text" name="custpwd" value="" placeholder="Enter User name">
</p>
<input type="submit" name="CreateAccount" id="create_account" value="Create">
</form>
</body>
</html>
PHP的 :
if(isset($_POST['CreateAccount'])){
$baseurl = "www.thirdpartyurl.com";
$username = "xxxxx";
$pw = "xxxx";
$customer = $_POST['custuname'];
$customerpw = $_POST['custpwd'];
$tariffrate=4;
$params = array("command"=>"createcustomer", "username"=>$username, "password"=>$pw,
"customer"=>$customer,"customerpassword"=>$customerpw,"tariffrate"=>$tariffrate);
$link = $baseurl . '?' . http_build_query($params);
$ch = curl_init();
// Set query data here with the URL
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
print $content;
}
?>
首先检查您是否将php文件托管在服务器上,因为如果脚本不正确,它必须给您一个错误,而不是显示相同的书面脚本。 这表明您尚未将php文件托管在wamp,xammp或tom cat等服务器上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.