Curl to wp_remote_post convert

I would like to convert these into 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);

I almost tried with this

Read More
$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);

But there is a error message like this – “Unable to parse json”

Please help

Thank You

Related posts

2 comments

  1. This works

        $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. Noticed a few inconsistencies. not sure if the syntax is throwing the errors but might fix the parsing….

    $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'
      )
    );
    

Comments are closed.