php中Curl的請求方式有哪些?php curl的四種請求方式介紹

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於php中Curl的請求方式有哪些?php curl的四種請求方式介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

1、發送json格式資料,請求地址:https

protected function https_request($url,$data=null){    $curl = curl_init();    curl_setopt($curl,CURLOPT_URL,$url);    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);    if(!empty($data)){        curl_setopt($curl,CURLOPT_POST,1);        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);    }    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);    //下面這行是修改後增加的代碼,就是配置設定host訪問,發送的資料類型為application/json    curl_setopt($curl, CURLOPT_HTTPHEADER, array(        'Content-Type: application/json; charset=utf-8',        'Content-Length: ' . strlen($data)    ));    $output = curl_exec($curl);    curl_close($curl);    return $output;}

2、發送json格式資料,請求地址:http

protected function curlPost($Url, $data){    $ch = curl_init($Url);    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON類型字串    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));    $result = curl_exec($ch);    curl_close ( $ch );    return $result;}

3、表單格式提交

function file_get_contents_post($url, $post){    $options = array(        'http'=> array(        'method'=>'POST',        'header' => "Content-type: application/x-www-form-urlencoded ",        'content'=> http_build_query($post),        ),    );    $result = file_get_contents($url,false, stream_context_create($options));    return $result;}$datare = file_get_contents_post("http://103.72.165.183/api/payment.aspx", $data);var_dump($datare);

4、$url是地址加資料的形式:http://baidu.com?a="ss"&b="ds";

public function getSSLHttp($url){        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_HEADER, false);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https請求 不驗證認證和hosts        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);        $data = curl_exec($curl);        $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);        if ( $httpCode != 200 ){            $data="https connect timeout";        }        curl_close($curl);        return $data;    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.