简体   繁体   中英

I need to send API request using get variables via HTTP requests using PHP

I need to send API request using get variables via HTTP requests using PHP.

Format is some thing like:

https:"www.thirdpartyurl.com"/Request.ashx?command=_&username= _&password=______&customer=__&customerpassword=___

Where 1. command = create customer

  1. username = my username

  2. password = my account password

  3. customer=customer user name(which he fills in the form)

  4. Customer password = customer password(as filed in the form)

Please have a look below for my HTML file :

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

My php file is :

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

When clicked on submit button instead of creating account it is showing me above PHP code in the browser. Kindly help me on this.

Should be

$customer = $_POST['custuname'];
$customerpw = $_POST['custpwd'];

instead of

$customer = "$_POST(custuname)";
$customerpw = "$_POST(custpwd); //<----quotes not closed

Can you try this, &tariffrate=4; should be $tariffrate=4; Added curl to send api request

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;

     }
  ?>

First check whether you have hosted your php file on a server or not because it must give you an error if the script is incorrect instead of showing the same written script. that indicates that you have not hosted your php file on a server like wamp, xammp or a tom cat etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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