Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

php - how to construct URL for API request?

I need help working with the lansweeper API. This is an example of their API request

http://YourServerName:81/api.aspx?Action=AddTicket&Key=123456789&Subject=Example&Description=Example&Username=example&email=test@blah.com

I have a HTML form below with the required values I need the user to input. The method has to be Post.I'm having trouble determining next steps from here.

I tried using hidden fields to add the Action and the Key but that wasn't working how I pictured it because i'm not sure how to hide the value and the identifier for it. I tried changing the form action to a php file, saving the input into variables and then tried constructing the link in a very roundabout way (below) but I got stuck once again not certain of where to go next.

Any help/insight is highly appreciated.

        <form action="http://lansweeper:81/api.aspx?" method="post">
        
            <input type="hidden" name="Action" value="AddTicket">
            <input type="hidden" name="Key" value="80aac06c-43c1-498a-9e0f-d477e398b13b">
        
            <label>Subject: </label>
            <input type="text"id="sub" name="Subject" required oninvalid="this.setCustomValidity('Name cannot be empty. Please enter your name')" 
            onchange="this.setCustomValidity('')">
            <br>
            
            <label>Description: </label>
            <input type="text"id="descrip" name="Descrip" required oninvalid="this.setCustomValidity('Name cannot be empty. Please enter your name')" 
            onchange="this.setCustomValidity('')"><br>
            
            <label>Username: </label>
            <input type="text"id="username" name="Username" required oninvalid="this.setCustomValidity('Name cannot be empty. Please enter your name')" 
            onchange="this.setCustomValidity('')"><br>
            
            <label>Email: </label>
            <input type="text"id="email" name="Email" required oninvalid="this.setCustomValidity('Name cannot be empty. Please enter your name')" 
            onchange="this.setCustomValidity('')"><br>
            
            
            <input type="submit">
            
        </form>

PHP:

        <?php
            if  (isset($_POST['submit'])){
                $link = "http://lansweeper:81/api.aspx?Action=";
                $sub = $_POST['subject'];
                $descrip = $_GET['desctip'];
                $user = $_POST['username'];
                $mail = $_POST['email'];
                $key = '123456789&';
                $action = 'AddTicket&';
                
                $link .= $action;
                $link .= $key;
                $link .= "Subject=";
                $link .= $sub;
                $link .= "&";
                $link .= "Description=";
                $link .= $descrip;
                $link .= "&";
                $link .= "Username=";
                $link .= $user;
                $link .= "&";
                $link .= "Email=";
                $link .= $mail;
                
                echo $link;
            }
    ?>

Documentation:

question from:https://stackoverflow.com/questions/65909677/how-to-construct-url-for-api-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...