如何PHP实现POST和GET请求的方法

php

在PHP中,您希望构造类似的GET和POST请求,这在某些APP或其他后端功能开发中是必需的。


  PHP语言提供了file_get_content()函数和curl支持。

  在PHP> 5.3版本中,默认情况下两者都启用。

  1. GET请求的实现:
  直接编写代码,不再赘述,有需要的朋友可以直接按Ctrl C Ctrl V

<?php
 
 
function httpGET($target_url, $get_data = array()){
    $result = @file_get_content($target_url.'/?'.http_build_query($get_data));
    return $result;
}
 
 
/*
*    方法2
*/
function httpGET2($target_url, $get_data = array()){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $target_url.'/?'.http_build_query($get_data));
    curl_setopt($curl, CURLOPT_HEADER, 1);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
     curl_close($curl);
    return $data;
}
 
?>
2、POST请求实现:
POST 请求没法儿使用 file_get_content() 函数来实现,所以只有使用 curl 方法来实现

<?php
 
function httpPOST($url , $post_data = array()){
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HEADER, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
  $response = curl_exec($curl);
  curl_close($curl);
  return $response;
}
?>

版权声明:《 如何PHP实现POST和GET请求的方法 》为作者风起兮原创文章,转载请注明原文地址!
最后编辑:2020-7-1 16:07:41
分享到:
赞(0)
发表评论 / Comment

用心评论~

金玉良言 / Appraise
Offilfent本站最强王者游客
2022-09-16 17:15
<a href=https://buydoxycyclineon.com/>doxycycline with azithromycin</a> The incidence of atrial fibrillation is type dependent, higher in ATTRwt 45 than in AL 12 or in ATTRm 15.
Tepeelaky本站最强王者游客
2022-09-13 02:44
Although we do not have data regarding duration of prenatal coverage for the women in the current study, women who were not covered in Kaiser during pregnancy, but who were in the Northern California region, would have been very likely to receive infertility care in one of the area s 11 other infertility clinics. <a href=http://tamoxifenolvadex.com/>tamoxifen belly</a> I have had the worst pelvic pain starting yesterday Sun and today Mon and I am not sure if it is from the Clomid or if something else is going on in there.
pypesmobe本站最强王者游客
2022-09-10 00:44
PUFFiTs are sleek, inhaler-style portable vapes made especially for dry materials. <a href=http://clomida.com/>clomid dosage for men</a> Samplaski MK, Loai Y, Wong K, Lo KC, Grober ED, et al.
Arrinty本站最强王者游客
2022-09-07 03:04
Garnering attention and sparking important discussions <a href=https://cheapcialiss.com/>cialis online without prescription</a>
endoday本站最强王者游客
2022-09-05 00:48
Since the coadministration of tadalafil and О±-adrenergic antagonists is safe when following the proper instructions, another question that arises is the possible role of combination treatment in patients with BPH-associated LUTS, especially using the new more uroselective О±-adrenergic antagonists <a href=https://vtopcial.com/>safe place to buy cialis online</a> Buy sale kamagra canada
Afforcify本站最强王者游客
2022-09-03 02:14
<a href=http://buypriligyo.com/>priligy ebay</a> However, some pharmacological drugs used for treating these comorbid conditions have sexual side effects
shamnex本站最强王者游客
2022-08-31 23:06
5mg 5mg tablet every day, any time you are sexually aroused or stimulated you can expect to have a hard and long-lasting erection <a href=https://cialisfstdelvri.com/>generic cialis online pharmacy</a>
embalia本站最强王者游客
2022-08-16 19:38
Levitra Pharmacocinetique <a href=http://iverstromectol.com/>stromectol no prescription</a> Propecia Mental Side Effects
ornalry本站最强王者游客
2022-07-28 05:22
<a href=http://buycialikonline.com>soft tab cialis</a> Fabricant Baclofene