$url = 'https://example.com/api/data'; $data = file_get_contents($url); echo $data;
$url = 'https://example.com/api/data'; $data = array('name' => 'John', 'email' => 'john@example.com'); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;
$url = 'https://example.com/api/data'; $data = array('name' => 'John', 'email' => 'john@example.com'); $options = array( 'http' => array( 'method' => 'PUT', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;This code updates data on the specified URL using the PUT method. The code is similar to the previous example, except that the method is changed to PUT. In terms of package libraries, one popular library for handling HTTP requests in PHP is Guzzle. Guzzle is a PHP HTTP client that simplifies the process of sending HTTP requests and processing responses. It provides an easy-to-use interface for making requests with various HTTP methods, including GET, POST, PUT, DELETE, and more. Guzzle also supports asynchronous requests and retries, as well as integration with various authentication methods.