PHP Communication & Stakeholder Relations 3 — Questions and Answers
Question 1: Which PHP extension provides the most common way to make HTTP requests to external APIs?
- soap
- curl (Correct answer)
- ftp
- xmlrpc
Correct answer: curl
The cURL extension is the most widely used PHP tool for making HTTP requests to external services and APIs.
Question 2: What is the correct way to set a custom HTTP response header for an API endpoint in PHP?
- echo 'Content-Type: application/json';
- header('Content-Type: application/json'); (Correct answer)
- response()->header('Content-Type', 'application/json');
- set_header('Content-Type', 'application/json');
Correct answer: header('Content-Type: application/json');
header() is the native PHP function for sending HTTP response headers before any output is flushed.
Question 3: In PHP's $_SERVER superglobal, which key holds the HTTP request method (GET, POST, etc.)?
- $_SERVER['METHOD']
- $_SERVER['HTTP_METHOD']
- $_SERVER['REQUEST_METHOD'] (Correct answer)
- $_SERVER['REQUEST_TYPE']
Correct answer: $_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_METHOD'] contains the HTTP method used for the current request.
Question 4: Which PHP function reads the raw body of an incoming HTTP request, useful for JSON API payloads?
- file_get_contents('php://input') (Correct answer)
- getallheaders()
- fread(STDIN, 4096)
- ob_get_contents()
Correct answer: file_get_contents('php://input')
file_get_contents('php://input') reads the raw POST body, which is necessary to parse JSON payloads sent to an API.
Question 5: What does CORS stand for in the context of PHP web APIs communicating with browsers?
- Cross-Origin Resource Sharing (Correct answer)
- Client-Origin Request Security
- Cross-Object Request Schema
- Client-Origin Response Spec
Correct answer: Cross-Origin Resource Sharing
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that PHP APIs handle via specific response headers.
Question 6: Which PHP function is used to redirect a user's browser to another URL?
- redirect()
- forward()
- header('Location: ...') (Correct answer)
- goto()
Correct answer: header('Location: ...')
header('Location: url') sends an HTTP redirect header; it must be followed by exit() to stop further execution.
Question 7: What PHP configuration directive controls the maximum size of POST data that can be received?
- upload_max_filesize
- post_max_size (Correct answer)
- max_post_data
- memory_limit
Correct answer: post_max_size
post_max_size in php.ini sets the maximum size of POST data allowed, affecting form submissions and API payloads.
Which PHP extension provides the most common way to make HTTP requests to external APIs?