关闭
dongwenghe2416 2014-04-29 08:13
浏览 72
已采纳

CURL不会将POSTFIELDS正确发送到某些服务器

I've tried searching, the other answers didn't help me.

I'm trying to send POST data via cURL, but it's only working on some servers. What gives?

I've tried:

if (!empty($data) && $usePost) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

$data contains an array with the key of products and the value of a JSON string.

I also tried explicitly using the query:

if (!empty($data) && $usePost) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}

But still, $_POST on the receiving server gives me NULL.

Any idea what to do?

Here's the whole method:

public static function execCommand($command, $ch,$data=array(),$cookie_file='genCookie.txt', $usePost = false) {
    $url = $command;
    if (!empty($data) && !$usePost)
        $url .= '?' . http_build_query($data);
    elseif (isset($data['sessionid'])) {
        $url .= '?sessionid=' . $data['sessionid'];
        unset($data['sessionid']);
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    if (!empty($data) && $usePost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    return curl_exec($ch);
}

I tried echoing $_REQUEST, so it's not in $_GET by mistake. I also tried file_get_contents('php://input'), but still nothing.

Both the sending and receiving ends are on Apache servers.

Bigger thing is, it's working on most servers, but only some are ignoring it. Is there a safer, more cross platform way to do this?

展开全部

  • 写回答

6条回答 默认 最新

  • dongyixiu3110 2014-05-14 06:47
    关注

    Turns out the problem, was the SSL on the receiving server. The request was being made to HTTP instead of HTTPS, and instead of giving an error or redirection, it simply dropped the POST on the way (maybe it redirected to HTTPS but failed to pass the POST along with it?)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部