1

I would like to build a PHP form pulling data using an API.
I have an API key and secret. I have done some basic test queries by manipulating the URL to make sure everything works and it does.

What I don’t know how to do is build the PHP form and display the results. I was going to do it in JS however then the API key and secret would be revealed in the URL and there are multiple search options.

From what I have seen with other people asking about php and api the connection would be like this:

$apikey = 'xxxx';
$secret = 'xxxx'
$service_url = 'https://website.com/' .'api_key=' . $apikey . '&api_secret='. $secret.  ‘&’ .$criteria . ‘&’ . $criteria2 . ‘&’ .$criteria3;

This is the API URL I use for testing:

domainnamehere/clients/api/search?api_key=1234&api_secret=1234&state=nsw&given_name=john&surname=smith

The results in json look like this:

[{"state":"NT","date":"2016-08-17","listing_type":"listing info","place":"place info","moreinfo":"","name":"SMITH Dale John","location":"Darwin","file":"1234","additional_info":"1  did something stupid"},{"state":"NT","date":"2016-08-03","listing_type":"listing info","place":"place info","moreinfo":"","name":"SMITH Dale John","location":"Katherine"," file":"4567","additional_info":"1 did some more dumb stuff;2 did even more dumb stuff;3  really dumb stuff;4  even more dumb stuff"}]

I have a basic understanding with PHP search forms however this appears to be well over my head. I am guessing it is going to be a mixture of submitting a query and pulling down data and formatting it.

What I am looking for is an answer on how to build this form. It will be a search form I put on a website and have the results displayed on the same page.

So it would look like this:

Search form:

Name: "enter name" Surname: "enter surname" State: "enter state"

Results.

State: QLD
Date: 01/01/2019
Listing Type: listing info
Place: Place info
More Info:
Name: Smith Dale John
Location: Darwin
File: 1234
additional_info: 1 did something stupid


State: QLD
Date: 01/01/2019
Listing Type: listing info
Place: Place info
More Info:
Name: Smith Dale John
Location: Darwin
File: 1234
additional_info: 1 did something stupid
2 did even more dumb stuff
3 really dumb stuff
4 even more dumb stuff


I know the search form itself should be relatively simple to do as it is just going to insert the form fields into the url.
But how to pull back the data from that url generated from the form submission and display them?

2
  • The url will be visible to the client that is passing the api information unless your passing something server side so I'm not sure what you're trying to hide. If worried about others knowing the API information, https should be used. As far as the json, I'm assuming you encoded it using json_encode but you can display easily by using json_decode'() as well. Depending on how you want to deliver this to the end user will determine how to present it to them (I know that's an obvious statement but...). Me? I'd spit out json and let them parse it as they see fit. Commented May 9, 2019 at 3:25
  • I was only worried about the url being visible if I used Javascript. I will add more info to my question to explain more. Thanks for the feedback. Commented May 9, 2019 at 3:37

1 Answer 1

1

To hide the api key and the api secret the you need add a new layer.

So you will have:

  1. Frontend layer (js/html) Here you will do an ajax request to an php script.

For more info on that check this link https://api.jquery.com/jquery.get/

  1. Backend layer (php) Here you will do a call to the API. The call can be done with curl.

For more info on that check this link https://www.php.net/manual/en/curl.examples.php

On frontend you will do the ajax to scrip.php?name=x&surname=y&state=z

On php you will get the variables with $_GET

Apikey and secret will only be used from php so those will be safe

Sign up to request clarification or add additional context in comments.

1 Comment

And of course the last part is for the PHP to receive the JSON data from the API and either a) pass that data straight back to the browser and let the JavaScript code work out how to display it in the page, or b) use the data to construct some HTML which is then passed back to the browser, and all the JavaScript code has to do is append it to the page somewhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.