php curl rest api example – cURL stands for ‘Client URL Library’. cURL API calls with PHP, REST and JSON data (GET POST PUT DELETE) Example with demo.
php curl rest api example – GET POST PUT DELETE
php curl rest api example – This Article will give you example of php curl post request with headers example with demo.
PHP cURL Basics
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "api.example.com"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl);
Don’t Miss : curl post request php
php curl rest api example
first of all set API URL and then Init cURL resource and pass Array Parameter Data after that pass encoded JSON string to the POST fields and set the content type json and set return type json & execute request and last close cURL resource.
PHP Curl POST Request with Headers Example
'infinityknow', 'email'=>'[email protected]']; curl_setopt($my_curl, CURLOPT_POSTFIELDS, $product_info); $headers = []; $headers[] = 'Content-Type:application/json'; curl_setopt($my_curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($my_curl); curl_close($my_curl); ?>
Header Auth Example:
'infinityknow', 'email'=>'[email protected]']; curl_setopt($my_curl, CURLOPT_POSTFIELDS, $product_info); $headers = [ 'Content-Type:application/json', 'App-Key: 89895656598989988989998899898', 'App-Secret: chjjdjd7847dffdg75fdgfd4g54fg4554f54gf5454gf']; curl_setopt($my_curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($my_curl); curl_close($my_curl); ?>
PHP cURL setup
function callRequest($method, $url, $product_info){ $my_ch = curl_init(); switch ($method){ case "POST": curl_setopt($my_ch, CURLOPT_POST, 1); if ($product_info) curl_setopt($my_ch, CURLOPT_POSTFIELDS, $product_info); break; case "PUT": curl_setopt($my_ch, CURLOPT_CUSTOMREQUEST, "PUT"); if ($product_info) curl_setopt($my_ch, CURLOPT_POSTFIELDS, $product_info); break; default: if ($product_info) $url = sprintf("%s?%s", $url, http_build_query($product_info)); } curl_setopt($my_ch, CURLOPT_URL, $url); curl_setopt($my_ch, CURLOPT_HTTPHEADER, array( 'APIKEY: 982560000221454878778778', 'Content-Type: application/json', )); curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($my_ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $result = curl_exec($my_ch); if(!$result){die("Connection Failure");} curl_close($my_ch); return $result; }
PHP cURL GET request
$products = callRequest('GET', 'https://www.domain-name.com/product/'.$user['Member']['visitor_id'], false); $result = json_decode($products, true); $errors = $result['result']['errors']; $data = $result['result']['data'][0];
PHP cURL POST request
$product_info = array( "customer" => $user['Member']['customer_id'], "product" => array( "code" => $this->request->data['code'], "description" => $this->request->data['description'], "qty" => $this->request->data['qty'] ), ); $saveProduct = callRequest('POST', 'https://www.domain-name.com/product_save/', json_encode($product_info)); $result = json_decode($saveProduct, true); $errors = $result['result']['errors']; $data = $result['result']['data'][0];
PHP cURL PUT request
$product_info = array( "price" => (string)($products['price'] / $discount) ); $update_plan = callRequest('PUT', 'https://www.domain-name.com/update_product/'.$products['product_id'], json_encode($product_info)); $result = json_decode($update_plan, true); $errors = $result['result']['errors']; $data = $result['result']['data'][0];
PHP cURL DELETE request
callRequest('DELETE', 'https://www.domain-name.com/delete_product/' . $product_id, false);
I hope you get an idea about php curl rest api example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.