ccc

CURL: LLamadas por POST, GET, PUT y DELETE

class CurlRequest {
public function sendPost() {
$data = array("a" => "a");
$ch = curl_init("http://localhost/curlRequest/api.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
    curl_close($ch);
if(!$response) {
return false;
}
else{
var_dump($response);
}
}

public function sendPut() {
$data = array("a" => "a");
$ch = curl_init("http://localhost/curlRequest/api.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    $response = curl_exec($ch);
curl_close($ch);
if(!$response) {
return false;
}
else{
  var_dump($response);
}
}

public function sendGet() {
  $data = array("a" => "a");
$ch = curl_init("http://localhost/curlRequest/api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
if(!$response) {
return false;
}
else{
var_dump($response);
}
}

public function sendDelete() {
  $data = array("a" => "a");
  $ch = curl_init("http://localhost/curlRequest/api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
if(!$response) {
return false;
}
else{
var_dump($response);
}
}
}


Más información en:
https://www.uno-de-piera.com/curl-peticiones-post-put-get-y-delete/

No hay comentarios:

Publicar un comentario