Klaitos
Klaitos

Reputation: 137

Send PUT request with PHP cURL

I'm trying to communicate with a web service who's waiting first a token in each request. There is my problem, the web service is waiting the token throught file_get_contenst(php://input).

I didn't find a way to send cURL request trought this method (sending raw data/put data instead of Post). Could some one guides me ?

I tried something like :

$fp = fopen('php://temp/maxmemory:256000', 'w');
fwrite($fp, TOKEN);
fseek($fp, 0);
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($json));

Upvotes: 2

Views: 16739

Answers (2)

alpha9eek
alpha9eek

Reputation: 1449

  1. HTTP PUT method is generally used to update resource.
  2. First need to bring (GET) resource information.
  3. and then update resource information according your parameters.
  4. Finally, make PUT request to server.

In php, curl is used for making rest calls. HTTP GET and POST are directly supported in curl. But for PUT and DELETE you have to set options accordingly in curl_setopt()

     $curl = curl_init($url);

     **curl_setopt($curl,CURLOPT_CUSTOMREQUEST,"PUT")**;
     curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);

     curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);

     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl,CURLOPT_POSTFIELDS,$update_para);

     $curl_response = curl_exec($curl);

Upvotes: 0

aaron
aaron

Reputation: 687

curl_setopt($ch, CURLOPT_PUT, true);

For more info you can refer: How to start a GET/POST/PUT/DELETE request and judge request type in PHP?

Upvotes: 7

Related Questions