curl get example

Example 1: php curl get

PHP cURL GET Request
A GET request retrieves data from a server. This can be a website’s HTML, an API response or other resources.

<?php

$cURLConnection = curl_init();

curl_setopt($cURLConnection, CURLOPT_URL, 'https://hostname.tld/phone-list');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$phoneList = curl_exec($cURLConnection);
curl_close($cURLConnection);

$jsonArrayResponse - json_decode($phoneList);

Example 2: curl get example

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

Example 3: curl example

curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'

Example 4: curl in api

curl is a command line client to send request and
get respond from command line, so it's basically 
it's just way to send a request and response. Where you
send it we call it client. But with postman we can do more
than just sending request end respond.
This tool is preferred for automation, 
since it is designed to work without user interaction.
curl can transfer multiple file at once.
Also A lot of API documentation provide curl
command for example when we run the request and
Postman can easily import curl command without you
manually typing the whole part of the request.

Example 5: curl get return code

curl -I http://www.example.org

Example 6: how to call api using curl method

// create & initialize a curl session
$curl = curl_init();

// set our url with curl_setopt()
curl_setopt($curl, CURLOPT_URL, "api.example.com");

// return the transfer as a string, also with setopt()
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// curl_exec() executes the started curl session
// $output contains the output string
$output = curl_exec($curl);

// close curl resource to free up system resources
// (deletes the variable made by curl_init)
curl_close($curl);

Tags: