How to turn this Curl / PUT command into php?

Asked

Viewed 3,638 times

3

How do I transpose this Curl code to send it in a PHP file?

curl -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -d
{
  "status":"paused"
}
https://api.mercadolibre.com/items/ITEM_ID?access_token=YOUR_ACCESS_TOKEN
  • is not the same thing...

  • this for example does not need in the request put: "curl_setopt($Curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($Curl, CURLOPT_SL_VERIFYHOST, 2); curl_setopt($Curl, CURLOPT_RETUNTRANSFER, true);"

  • I’m having difficulty creating the body of the request: header , data (how to send via json), etc; I don’t know what the syntax looks like...

  • Well, I reopened, but if you follow the logic there (as well as the link that points to all Curl options), I think you can easily get it. I have the impression that it will be faster than waiting for someone to pick up exactly your request and adapt, but in any way, it is open.

  • Headers: curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'header1: value1', 'header2: value2' ));

  • 1

    It could be uncomplicated using the official library: https://github.com/mercadolibre/php-sdk

Show 1 more comment

2 answers

4

$url       = 'https://api.mercadolibre.com/items/ITEM_ID?access_token=YOUR_ACCESS_TOKEN';
$cabecalho = array('Content-Type: application/json', 'Accept: application/json');
$campos    = json_encode(array('status' => 'paused'));

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,     $cabecalho);
curl_setopt($ch, CURLOPT_POSTFIELDS,     $campos);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,           true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  'PUT');

$resposta = curl_exec($ch);

curl_close($ch);
  • 1

    It would really be necessary (or advisable) to set CURLOPT_POST?

-1

Perfect solution works perfectly just forgot the point and comma

$url       = 'https://api.mercadolibre.com/items/ITEM_ID?access_token=YOUR_ACCESS_TOKEN';
$cabecalho = array('Content-Type: application/json', 'Accept: application/json')    <<<<--------   adicionar o   ";"
$campos    = json_encode(array('status' => 'paused'));

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,     $cabecalho);
curl_setopt($ch, CURLOPT_POSTFIELDS,     $campos);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,           true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  'PUT');

$resposta = curl_exec($ch);

curl_close($ch);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.