If you don't find an answer, please click here to post your question.

Creating a PUT/POST/PUT Request Using PHP

0 Kudos
Started ‎08-27-2012 by
Modified ‎10-07-2013 by

Creating a PUT/POST/PUT Request Using PHP

by on ‎08-27-2012 06:06 PM - edited on ‎10-07-2013 10:33 AM by Community Manager (5,556 Views)

TS-00045

 

This article explains how to make a PUT/POST request to a URL with a request body specified in JSON format.

HOW TO RESOLVE

Here add the steps taken to resolve the issue using curl-php.

RESOLUTION 1

Code:

<?php

$content = “”; //Json formatted content (Eg.  $content = "{\"asset_type\":\"video\"}”)

 $url=’’’; //URL to which you are making the call to

 

$ch = curl_init($url);

                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json"));

                //Can be PUT/POST/PATCH

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

                curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

 

                try {

                        return httpRequest($ch);

                } catch (Exception $e) {

                        throw $e;

                }

        

function httpRequest($ch)

        {

               curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

                $response = curl_exec($ch);

 

                if(curl_error($ch))

                        {

                        curl_close($ch);

                        return curl_error($ch);

                        }

 

                $head=curl_getinfo($ch);

               $content = $head["content_type"];//application/json

                $code = $head["http_code"];//200

                curl_close($ch);

        }

?>