【问题标题】:Curl to wp_remote_post convert卷曲到 wp_remote_post 转换
【发布时间】:2015-10-02 14:49:35
【问题描述】:

我想把这些转换成 wp_remote_post()

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->url).'","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($curl);
curl_close ($curl);
$json = json_decode($curl_results, true);

我几乎尝试过这个

$params = array(
    'method' => 'POST',
    'timeout' => 45,
    'blocking' => true,
    'headers' => array(
        'Content-Type' => 'application/json'
    ),
    'body' => array(
        'method' => 'pos.plusones.get',
        'id' => 'p',
        'params'=> array (
            'nolog' => true,
            'id' => rawurldecode($url),
            'source' => 'widget',
            'userId' => '@viewer',
            'groupId' => '@self',
        ),
        'jsonrpc' => '2.0',
        'key' => 'p',
        'apiVersion' => 'v1',
    ),
);
$connection = wp_remote_post('https://clients6.google.com/rpc', $params);

但是有这样的错误信息——“无法解析json”

请帮忙

谢谢

【问题讨论】:

    标签: php wordpress curl


    【解决方案1】:

    这行得通

        $params     = array(
        'method'   => 'POST',
        'timeout'  => 45,
        'blocking' => true,
        'headers'  => array(
            'Content-Type' => 'application/json'
        ),
        'body'     => '['.json_encode( array(
                'method'     => 'pos.plusones.get',
                'id'         => 'p',
                'params'     => array(
                    'nolog'   => true,
                    'id'      => rawurldecode( $url ),
                    'source'  => 'widget',
                    'userId'  => '@viewer',
                    'groupId' => '@self',
                ),
                'jsonrpc'    => '2.0',
                'key'        => 'p',
                'apiVersion' => 'v1',
            ) ).']'
    );
    $connection = wp_remote_post( 'https://clients6.google.com/rpc', $params );
    

    【讨论】:

      【解决方案2】:

      注意到一些不一致之处。不确定语法是否引发错误,但可能会修复解析....

      $params = array(
        'method' => 'POST',
        'timeout' => 45,
        'blocking' => true,
        'headers' => array(
          'Content-Type' => 'application/json'
        ),
        'body' => array(
          'method' => 'pos.plusones.get',
          'id' => 'p',
          'params'=> array(
            'nolog' => true,
            'id' => rawurldecode($url),
            'source' => 'widget',
            'userId' => '@viewer',
            'groupId' => '@self'
          ),
          'jsonrpc' => '2.0',
          'key' => 'p',
          'apiVersion' => 'v1'
        )
      );
      

      【讨论】:

        猜你喜欢
        • 2017-06-03
        • 2013-10-21
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多