【问题标题】:WordPress cURL and wp_remote_postWordPress cURL 和 wp_remote_post
【发布时间】:2015-03-11 05:22:59
【问题描述】:

所以我的问题是直到现在我在我的一个 wordpress 插件 cURL 中使用了 POST 请求,但现在我需要使用 wp_remote_post()。

wp_remote_post 看起来很简单,但我无法让它工作。所以我的问题是:有人可以告诉我如何将以下cURL 转移到wp_remote_post 吗?

卷曲:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
$result = curl_exec($ch);
curl_close($ch);

我的 wp_remote_post 版本

$result = wp_remote_post($url, array(
    'method' => 'POST',
    'headers' => $headers,
    'body' => json_encode($fields) )
);

我收到wp_remote_post 的 401 错误,因为授权无效。

【问题讨论】:

    标签: php wordpress post curl


    【解决方案1】:

    我解决了。出于某种原因,在添加 httpversion 和 sslverify 之后,它现在正在工作。希望这可以帮助某人:

    $result = wp_remote_post($url, array(
            'method' => 'POST',
            'headers' => $headers,
            'httpversion' => '1.0',
            'sslverify' => false,
            'body' => json_encode($fields))
        );
    

    【讨论】:

      【解决方案2】:

      以前的答案对我不起作用。 也许是因为它是从 2015 年开始的。

      我正在使用我的 WordPress 插件中的wp_remote_post()。 没有curl() 电话。

      以下确实有效,请注意添加的一些内容:超时、重定向和阻塞。 WP 5+

      $result = wp_remote_post($url, array(
          'method' => 'POST',
          'headers' => $headers,
          'timeout'     => 60, // added
          'redirection' => 5,  // added
          'blocking'    => true, // added
          'httpversion' => '1.0',
          'sslverify' => false,
          'body' => json_encode($fields))
      );
      

      【讨论】:

        猜你喜欢
        • 2012-06-21
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 2021-10-05
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多