PHP: using curl for get and post

PHOTO EMBED

Tue Aug 30 2022 13:47:58 GMT+0000 (UTC)

Saved by @marcopinero #php

<?php
//--- GET
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, 'https://url-de-la-api/test?param1=value'); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
   curl_setopt($ch, CURLOPT_HEADER, 0); 
   $data = curl_exec($ch); 
   curl_close($ch); 
?>

<?php
//--- POST
    $fields = array('field1' => 'valor1', 'field2' => urlencode('valor 2'));
    $fields_string = http_build_query($fields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://url-de-la-web/test");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string );
    $data = curl_exec($ch);
    curl_close($ch);
?>
content_copyCOPY