Curl to wp_remote_post convert

I would like to convert them to 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

$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 an error message like this: "Unable to parse json"

Please, help

thank

+3


source to share


2 answers


It 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 );

      

+1


source


I noticed a few inconsistencies. not sure if the syntax is throwing errors, but can fix 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'
  )
);

      

0


source







All Articles