Heim >Backend-Entwicklung >PHP-Tutorial >Was sind die Anfragemethoden von Curl in PHP? Einführung in die vier Anforderungsmethoden von PHP Curl

Was sind die Anfragemethoden von Curl in PHP? Einführung in die vier Anforderungsmethoden von PHP Curl

不言
不言Original
2018-09-10 14:35:322450Durchsuche

In diesem Artikel erfahren Sie, was die Anforderungsmethoden von Curl in PHP sind. Die Einführung der vier Anforderungsmethoden von PHP Curl hat einen gewissen Referenzwert. Freunde in Not können sich darauf beziehen.

1. JSON-Formatdaten senden, Adresse anfordern: 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-Formatdaten senden, Adresse anfordern: 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 ist die Anrede plus Daten: 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;
    }

Verwandte Empfehlungen :

Wie PHP Curl http- und https-Anfragen implementiert, phpcurlhttps-Anfrage

Drei Möglichkeiten für PHP, POST-Anfragen zu senden

Das obige ist der detaillierte Inhalt vonWas sind die Anfragemethoden von Curl in PHP? Einführung in die vier Anforderungsmethoden von PHP Curl. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

In Verbindung stehende Artikel

Mehr sehen