Есть ли более простой способ конвертировать 'curl_setopt' в массив wp_remote-post $ args? - PullRequest
2 голосов
/ 06 июля 2019

Мне нужно преобразовать мой функционирующий 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. Любая помощь будет очень цениться;Я занимаюсь этим часами.

1 Ответ

0 голосов
/ 06 июля 2019
  1. Странно, но превращение массива заголовков в ассоциативный массив в wp_remote_post решило проблему. На curl_setopt это не так.

    $ this-> args ['headers'] = массив ('Authorization: Basic' => $ authHeader);

...