Examples of GET and POST methods in php using http calls

  • 2021-07-21 07:35:01
  • OfStack

The functions used are curl_init, curl_setopt, curl_exec, curl_close.

The default is the GET method, and you can choose whether to use Header or not:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_HEADER, 1); // If set to 0 Do not use the header
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);

POST method:


$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,' $ url');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$vars =sprintf('from=%d&to=%d&subject=%s&body=%s',$from, $to, urlencode($subject), urlencode($body));
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$ret = curl_exec($ch);
curl_close($ch);

Related articles: