Building REST APIs on LAMP Flashcards
(25 cards)
What is a REST API in the context of LAMP?
A REST API is a web service using HTTP methods (GET, POST) to perform CRUD operations on a LAMP stack.
In WordPress, REST APIs (e.g., /wp-json/) enable data exchange. Freelancers build APIs for client integrations, while enterprise architects design scalable APIs for enterprise systems, aligning with your PHP APIs and WordPress APIs decks.
Why use REST APIs in LAMP applications?
REST APIs enable flexible, stateless communication between clients and LAMP servers.
For WordPress, APIs support mobile apps or headless setups. Freelancers create APIs for custom features, while enterprise architects ensure interoperability, per your headless WordPress interest.
How do you start building a REST API in PHP on LAMP?
Create a PHP script to handle HTTP requests, like if ($_SERVER[‘REQUEST_METHOD’] === ‘GET’) { echo json_encode([‘data’ => ‘Hello’]); }.
Forms the basis for WordPress or custom APIs. Freelancers write endpoints, while enterprise architects use frameworks, per your PHP core concepts deck.
What is the WordPress REST API in LAMP?
The WordPress REST API provides endpoints like /wp-json/wp/v2/posts for data access.
Runs on LAMP to serve WordPress content. Freelancers extend it for clients, while enterprise architects customize it for enterprise integrations, per your WordPress APIs deck.
How do you create a custom REST endpoint in WordPress on LAMP?
Use register_rest_route(), like register_rest_route(‘myapi/v1’, ‘/data’, [‘methods’ => ‘GET’, ‘callback’ => ‘my_callback’]);.
Adds WordPress API endpoints. Freelancers build custom routes, while enterprise architects secure them, per your PHP APIs deck.
What is a REST API route in PHP?
A route maps a URL to a handler, like /api/users to a PHP function.
In WordPress, routes define endpoints. Freelancers create routes, while enterprise architects design structured APIs, per your API development experience.
How do you handle GET requests in a PHP REST API?
Check $_SERVER[‘REQUEST_METHOD’] === ‘GET’ and return JSON, like echo json_encode([‘users’ => $data]);.
Retrieves WordPress data. Freelancers implement GET endpoints, while enterprise architects optimize responses, per your PHP core concepts.
How do you handle POST requests in a PHP REST API?
Check $_SERVER[‘REQUEST_METHOD’] === ‘POST’ and process $_POST or php://input.
Creates WordPress data (e.g., posts). Freelancers handle submissions, while enterprise architects validate inputs, per your security deck.
What is json_encode() in PHP REST APIs?
json_encode() converts PHP data to JSON, like $json = json_encode([‘name’ => ‘John’]);.
Formats WordPress API responses. Given your array experience, freelancers use it for output, while enterprise architects ensure proper encoding, per your PHP APIs deck.
What is json_decode() in PHP REST APIs?
json_decode() converts JSON to PHP data, like $data = json_decode($input, true);.
Parses WordPress API inputs. Freelancers process requests, while enterprise architects validate data, per your PHP APIs deck.
How do you secure a REST API in LAMP?
Use HTTPS, API keys, OAuth, and validate inputs with filter_var().
Protects WordPress APIs. Freelancers secure endpoints, while enterprise architects enforce authentication, per your WordPress security deck.
What is an API key in a LAMP REST API?
An API key is a token for authenticating requests, like ?api_key=12345.
Secures WordPress API access. Freelancers implement keys, while enterprise architects use secure storage, per your PHP security deck.
How do you implement OAuth in a LAMP REST API?
Use a library like league/oauth2-server to issue access tokens.
Enables secure WordPress API authentication. Freelancers set up OAuth, while enterprise architects integrate with SSO, per your PHP APIs deck.
How do you configure Apache for a REST API in LAMP?
Enable mod_rewrite and set .htaccess to route requests, like RewriteRule ^api/ index.php [L].
Supports WordPress or custom API URLs. Freelancers configure Apache, while enterprise architects optimize routing, per your Apache deck.
How do you connect a REST API to MySQL in LAMP?
Use PDO, like $pdo->prepare(“SELECT * FROM wp_posts WHERE ID = ?”)->execute([$id]);.
Fetches WordPress data. Freelancers write queries, while enterprise architects use prepared statements, per your MySQL and PHP database decks.
What is rate limiting in a LAMP REST API?
Rate limiting restricts request frequency, configured in Apache or PHP.
Protects WordPress APIs from abuse. Freelancers use plugins, while enterprise architects implement throttling, per your performance deck.
How do you implement rate limiting in Apache for LAMP?
Use mod_ratelimit, like SetOutputFilter RATE_LIMIT; SetEnv rate-limit 100.
Limits WordPress API requests. Freelancers configure limits, while enterprise architects scale throttling, per your Apache deck.
What is caching in a LAMP REST API?
Caching stores API responses, using tools like memcached or HTTP headers.
Speeds up WordPress API calls. Freelancers enable caching, while enterprise architects optimize cache strategies, per your performance deck.
How do you cache API responses in PHP on LAMP?
Use memcached, like $memcached->set(‘key’, $data, 3600);.
Caches WordPress API data. Freelancers implement caching, while enterprise architects scale it, per your LAMP performance deck.
How do you handle errors in a LAMP REST API?
Return HTTP status codes (e.g., 404, 500) and JSON errors, like http_response_code(404); echo json_encode([‘error’ => ‘Not found’]);.
Informs WordPress API clients. Freelancers handle errors, while enterprise architects log them, per your error handling deck.
What is CORS in a LAMP REST API?
CORS (Cross-Origin Resource Sharing) allows cross-domain requests, set with Header set Access-Control-Allow-Origin “*”.
Enables WordPress API access from other domains. Freelancers configure CORS, while enterprise architects restrict origins, per your security deck.
How do you test a REST API in LAMP?
Use tools like Postman or curl, like curl -X GET http://localhost/api/users.
Validates WordPress API endpoints. Freelancers test functionality, while enterprise architects automate tests, per your debugging deck.
How do you document a LAMP REST API?
Use OpenAPI/Swagger to define endpoints, like /users: get: description: List users.
Documents WordPress APIs. Freelancers create docs, while enterprise architects standardize documentation, per your API development goals.
How do you deploy a REST API in LAMP?
Use Git to push code, rsync to sync files, and restart Apache with sudo service apache2 restart.
Deploys WordPress APIs. Freelancers manage deployments, while enterprise architects use CI/CD, per your LAMP deployment deck.