PHP CURL POST 请求和 417 错误

5
function query($url, $pfields = 0, $cookie = 0)
{
    curl_setopt($ch, CURLOPT_HEADER, 1);
    if (!empty($pfields))
    {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $pfields);
    }            
    if (!empty($cookie))
    {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);            
    }            
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_ENCODING,'gzip');
    if (!$login)
    {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    }
    $content = curl_exec($ch);
    return $content;
}

$cookie = 'sessionID=3864cab58412ec567b634db3c317898;OAGEO=RU%7C%7C%7C%7C%7C%7C%7C%7C%7C%7C;';
$p = '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++';
$post = 'clientid=23&campaignid=52&bannerid=111&appendsave=1&appendtype=0&append=' . urlencode($p) . '&submitbutton=';

echo query('http://example.com/in.php', $post, $cookie);

这段代码返回417错误(但$p没有使用urlencode,但是使用加号+代替空格是可以的)

非常抱歉我的英语很糟糕


1
理论上你的代码可能应该正常工作,417状态码是为错误的 Expect 头保留的,而你没有发送。服务器本身可能在表现不良,你可以尝试通过使用 curl_setopt($ch, CURLOPT_VERBOSE, 1); 获取额外的数据来了解其失败原因。 - Wrikken
Curl会自动为“大”帖子添加它。某些服务器,如lighttpd,不支持它。 - Matthew
1个回答

13

尝试添加以下内容:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

有没有可能解释一下为什么这个能够工作? - Nate
2
@Nate,Curl自动为“大”帖子添加Expect/100-Continue头。简而言之,这允许客户端在发布大块数据之前向服务器请求权限。(例如,如果服务器将拒绝您的请求,则为什么要发布数据?)一些服务器,特别是lighttpd,不支持这一点,而不是静默失败,它们会产生错误。上述代码只需清除该标头并发送常规请求即可。 - Matthew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接