【发布时间】: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