Send PUT request with PHP cURL
I am trying to communicate with a web service that waits for a token in every request for the first time. There is my problem, the web service is waiting for a token via tt file_get_contenst(php://input)
.
I have not found a way to send a cURL request by calling this method (send raw data / put data instead of Post). Can anyone visit me?
I've 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));
source to share
curl_setopt($ch, CURLOPT_PUT, true);
For more information, you can refer to: How to run GET / POST / PUT / DELETE request and type of judgment request in PHP?
source to share
- The HTTP method is
PUT
commonly used to update a resource. - First you need to bring (
GET
) information about the resources. - and then update the resource information according to your parameters.
- Finally, make a request
PUT
to the server.
In php, curl is used to call rest. HTTP GET
and are POST
directly supported in curl. But for PUT and DELETE, you must set the parameters accordingly incurl_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);
source to share