【问题标题】:is there an easier way to convert 'curl_setopt' to wp_remote-post $args array?有没有更简单的方法将 'curl_setopt' 转换为 wp_remote-post $args 数组?
【发布时间】:2019-11-16 19:35:33
【问题描述】:

我需要将功能正常的 curl_exec() 转换为 WordPress wp_remote_get()

我尝试了几个参数选项,但一直收到 401

//CURL code 'this runs perfectly':
$endpoint = $this->_getApiEndpointForUser($username);
$authHeader = base64_encode($username . ':' . $apiKey);//
$this->curl = curl_init($endpoint);//
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $authHeader));//
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_CAINFO, dirname(__FILE__) . '/geotrust_global_ca.crt');
$payload = array('method' => $method, 'params' => $params, 'id' => $this->_generateRequestId(),);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,    $this->json_encode($payload));
$result = curl_exec($this->curl);

//wp_remote_post code 'This returns a 401':
$this->endpoint = self::_getApiEndpointForUser($username);
$authHeader = base64_encode($username . ':' . $apiKey);
$this->args['headers'] = array('Authorization: Basic ' . $authHeader);
$this->args['cookies'] = array();
$this->args['sslverify'] = true;
$this->args['sslcertificates'] = dirname(__FILE__) . '/geotrust_global_ca.crt';
$payload = array('method' => $method, 'params' => $params, 'id' => $this->_generateRequestId());
$this->args['body'] = json_encode($payload);
$response = wp_remote_post($this->endpoint, $this->args);

我希望在运行此程序时得到 200 响应代码,但继续得到 401。任何帮助将不胜感激;我已经在这里待了好几个小时了。

【问题讨论】:

    标签: php wordpress authentication plugins php-curl


    【解决方案1】:
    1. 很奇怪,但是在 wp_remote_post 中将 headers 数组转换为关联数组解决了问题。在 curl_setopt 上,这不是真的。

      $this->args['headers'] = array('Authorization: Basic ' => $authHeader);

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 2011-02-07
      • 2020-12-02
      • 2023-03-18
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 2022-07-08
      • 2010-12-07
      相关资源
      最近更新 更多