php curl的get-post方式

源代码
/**
 * post请求
 * @param $url
 * @param $postdata
 * @return mixed
 */
function get_component($url,$postdata){

    $ch = curl_init(); //用curl发送数据给api
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
    $response = curl_exec($ch);
    curl_close( $ch );
    $rest = json_decode($response,true);
    return $rest;
}
/**
 * post请求 row形式
 * @param $url
 * @param $postdata
 * @return mixed
 */
function post_row($url,$post_data){
$ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($post_data)
                )
            );
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            return $response;
}


/**
 * get请求
 * @param $url
 * @return mixed
 */
function curlget($url){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
    $response = curl_exec($ch);
    curl_close( $ch );
    $rest = json_decode($response,true);
    return $rest;
}

posted @ 2020-07-18 17:08  办公魔盒  阅读(212)  评论(0)    收藏  举报