鍍金池/ 問(wèn)答/PHP/ PHP用curl請(qǐng)求一個(gè)post的接口,那邊卻接收不到數(shù)據(jù)

PHP用curl請(qǐng)求一個(gè)post的接口,那邊卻接收不到數(shù)據(jù)

這是我封裝的post的curl方法

  
    static function PostCurl($uri, $params = array())
    {
        $url = self::CURL_URI . $uri;
        $params['pcs'] = 3;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);//設(shè)置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $output = curl_exec($ch);
        curl_close($ch);     
        return json_decode($output,true);
    }
回答
編輯回答
兮顏

`
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
`
$headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);把headers打印出來(lái)可以出加與不加http_build_query()是有區(qū)別的。

2017年5月11日 10:48
編輯回答
野橘

個(gè)人覺(jué)得,他那邊可以檢查一下nginx日志和php執(zhí)行日志,另外你這邊也要檢查一下nginx日志和PHP執(zhí)行日志。

2018年7月8日 20:10
編輯回答
乞許

你的參數(shù)試一下 json格式和array的格式。

還不行,可能是編碼的問(wèn)題。

2017年7月9日 21:02
編輯回答
旖襯

另外一邊打印個(gè)日志,看有沒(méi)有走過(guò)去,會(huì)不會(huì)url有問(wèn)題

2018年3月26日 14:35
編輯回答
落殤
function curls($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); // 對(duì)認(rèn)證證書(shū)來(lái)源的檢查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 從證書(shū)中檢查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;
}

試試這個(gè)

2017年2月4日 12:59