首页 > 解决方案 > 使用 wp_remote_post 在 Wordpress 中发布数据时,cookie 和/或 postfields 中的错误配置会导致此 500 内部服务器错误吗?

问题描述

我正在连接到一个没有发布 API 的服务器,并且已经成功地能够使用 PHP curl 函数来检索令牌和登录,但是当我尝试使用 Wordpress 内置函数来实现同样的事情时,我不成功并从远程主机收到 500 内部服务器错误。我相信我可能需要正确配置 cookie 和/或数据参数。

我可以使用 wp_remote_get 从远程主机检索令牌。我尝试将 Content-Type 更改为 text、json 和其他常见的替代方案,尽管使用 curl 时“application/x-www-form-urlencoded”工作正常。我尝试在标头和帖子字段中发送令牌。

// I am trying to get this to work:

$token = get_transient('access_token');
if (!$token)
    return;

$args = array(
    'method'      => 'POST',
    'timeout'     => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'compress'    => true,
    'headers'     => array(
        'Authority' => 'remote.site.com',
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Referer' => 'remote.site.com/login',
        'Cache-Control' => 'max-age=0',
        'Upgrade-Insecure-Requests' => 1,
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
        'Accept-Encoding' => 'gzip, deflate, br',
        'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5',
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
    ),
    'cookies'     => array(),
    'body'        => array(
        '__RequestVerificationToken' => $token,
        'UserName' => USERNAME,
        'Password' => PASSWORD,
        'RememberMe' => true
    )
);

$response = wp_remote_post( 'remote.site.com', $args);

// 这样做没有问题...

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'remote.site.com/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__RequestVerificationToken=$token&UserName=JohnDoe&Password=password&RememberMe=true");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'Authority: remote.site.com';
$headers[] = 'Cache-Control: max-age=0';
$headers[] = 'Origin: https://remote.site.com';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Referer: https://remote.site.com/login';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_HEADER, 1);

使用 curl 函数时,我得到登录请求的 302 响应并且后续请求得到满足,而使用 wp_remote_post 我得到登录页面的响应和 500 内部服务器错误响应。

标签: phpwordpressrestcurl

解决方案


add code inside the wp-config file 

定义('COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );

define( 'ADMIN_COOKIE_PATH', '/' );  

定义('COOKIEPATH','/');

定义('SITECOOKIEPATH','/');


推荐阅读