当前位置 : 主页 > 网络编程 > PHP >

php调用python接口,curl请求

来源:互联网 收集:自由互联 发布时间:2023-09-03
curl调用其他语言接口时多少会有些区别,以下是请求python的,一是方便大家也是方便自己以后使用 function curls_python($url, $params = false, $ispost = 1, $https = 0){ $httpInfo = array(); $ch = curl_init()

curl调用其他语言接口时多少会有些区别,以下是请求python的,一是方便大家也是方便自己以后使用

function curls_python($url, $params = false, $ispost = 1, $https = 0)

{
    $httpInfo = array();

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if ($https) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

    }

    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

        curl_setopt($ch, CURLOPT_URL, $url);

    } else {
        if ($params) {
            if (is_array($params)) {
                $params = http_build_query($params);

            }

        curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);

        } else {
            curl_setopt($ch, CURLOPT_URL, $url);

        }

    }

    $response = curl_exec($ch);

    if ($response === FALSE) {
        return false;

    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));

    curl_close($ch);

    return $response;

}

以上curl就可以对python接口请求了

网友评论