PHP code example of denniscarrazeiro / php-curl-module
1. Go to this page and download the library: Download denniscarrazeiro/php-curl-module library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
denniscarrazeiro / php-curl-module example snippets
use DennisCarrazeiro\Php\Curl\Module\Curl\Curl;
$curl = new Curl();
$response = $curl->url('https://api.example.com/users')
->returnTransfer(true) // Returns the response as a string
->execute();
if ($curl->statusCode() === 200) {
echo "Successful GET request!\n";
// Process the response (usually in JSON)
$data = json_decode($response, true);
print_r($data);
} else {
echo "GET request failed. Status code: " . $curl->statusCode() . "\n";
if ($errors = $curl->getValidationsErrors()) {
echo "Error details: " . implode(", ", $errors) . "\n";
}
}
use \DennisCarrazeiro\Php\Curl\Module\Curl\Curl;
$data = [
'name' => 'New User',
'email' => '[email protected]'
];
$jsonData = json_encode($data);
$curl = new Curl('application/json'); // Sets the Content-Type in the constructor
$response = $curl->url('https://api.example.com/users')
->customRequest('POST') // Sets the method to POST
->postFields($jsonData) // Sends the JSON data in the body
->addHeader('X-API-Key: your_api_key') // Adds a custom header
->returnTransfer(true)
->execute();
if ($curl->statusCode() === 201) { // Status code 201 usually indicates successful creation
echo "User created successfully!\n";
$responseData = json_decode($response, true);
print_r($responseData);
} else {
echo "Error creating user. Status code: " . $curl->statusCode() . "\n";
if ($errors = $curl->getValidationsErrors()) {
echo "Error details: " . implode(", ", $errors) . "\n";
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.