如何将wp_emote_post中的正文作为“RAW”发送?

时间:2020-02-10 作者:Brendan Quigley

我不确定我问的问题是否正确,所以这里是上下文。

我正在发布一个API(该API是一个Google Sheets脚本,因此有一些不寻常的限制)。当我使用postman时,数据以json文件的形式返回。为了让它在postman中工作,我必须通过body发送参数,并且必须选择raw选项。选择任何其他选项(即x-www-form-urlencoded或form data)将导致html错误页面。

我已经遵循了wp\\u remote\\u post($api\\u url,$body)的步骤;在WordPress文档中-但我得到的是html错误,而不是预期的数据。

我怎样才能像邮递员一样把尸体“生的”送去呢?

更新-添加代码。

$api_url = \'https://script.google.com/macros/s/UNIQUE_KEYw/exec\';
// $body = array(
//  "method"=>"GET",
//  "sheet"=>"date",
//  "key"=>"PASSWORD");

$body = (\'{\\n"method": "GET",\\n"sheet": "date",\\n"key": "PASSWORD"\\n}\');

$request =  wp_remote_post($api_url , array(
    \'method\'      => \'POST\',
    \'headers\'     => ["Content-Type" => "raw"],
    \'body\'        => $body,
    \'data_format\' => \'body\'
    )
                          );
我试图重新创建的来自postman的HTTP\\U Request2片段。

<?php
require_once \'HTTP/Request2.php\';
$request = new HTTP_Request2();
$request->setUrl(\'https://script.google.com/macros/s/UNIQUE_KEY/exec\');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
  \'follow_redirects\' => TRUE
));
$request->setHeader(array(
  \'Content-Type\' => \'text/plain\'
));
$request->setBody(\'{\\n"method": "GET",\\n"sheet": "date",\\n"key": "PASSWORD"\\n}\');
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo \'Unexpected HTTP status: \' . $response->getStatus() . \' \' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo \'Error: \' . $e->getMessage();
}

2 个回复
SO网友:Mark Dave Alonzo

我让它工作了,您必须在请求的body参数上传递json\\u编码的数组。

 $customer_details = [
            \'first_name\' => $first_name,
            \'last_name\' => $last_name,
            \'email\' => $email,
        ];

        $customer_details[\'body\'] = json_encode($customer_details);

        $customer = $api->createUpdateCustomer($customer_details);

public function createUpdateCustomer($customer_details) {
        $response = $this->apiCall( "2.0/customers", \'POST\', $customer_details);
        $result = json_decode($response);
        return $result;
    }

public function apiCall($baseUrl = null, $method = \'GET\', $args = array()) {
        if ( !is_null( $baseUrl ) ) {
            $url = $this->baseUrl . $baseUrl;
        }
        else {
            $url = $this->baseUrl;
        }

        // Populate the args for use in the wp_remote_request call
        $wp_args = array_merge($args, $this->args);
        $wp_args[\'method\']  = $method;
        $wp_args[\'timeout\'] = 30;        

        // Make the call and store the response in $res
        $res = wp_remote_request( $url, $wp_args );        

        // Check for success
        if ( ! is_wp_error( $res ) && ( 200 == $res[\'response\'][\'code\'] || 201 == $res[\'response\'][\'code\'] ) ) {
            return $res[\'body\'];
        } else {
            throw new Exception( "API call didn\'t go well :(. Either VendHQ Connect Settings are incorrect or VendHQ server is not responding as expected." );
        }
    }

SO网友:Tim Elsass

您应该能够设置headers 使用content-type: application/json:

$api_url = \'https://script.google.com/macros/s/UNIQUE_KEYw/exec\';
$body = wp_json_encode( array(
    "method" => "GET",
    "sheet" => "date",
    "key" => "PASSWORD"
) );

wp_remote_post( $api_url, [
    \'headers\'   => [ \'Content-Type\' => \'application/json\' ],
    \'body\'       => $body,
    \'data_format\' => \'body\'
] );