Curl in wp_remote_post umwandeln

Lesezeit: 2 Minuten

Ich möchte diese in wp_remote_post() umwandeln

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

Ich habe es fast damit versucht

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

Aber es gibt eine Fehlermeldung wie diese – “Unable to parse json”

Bitte helfen Sie

Danke

Benutzer-Avatar
Francesco

Das funktioniert

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

Habe ein paar Ungereimtheiten bemerkt. Ich bin mir nicht sicher, ob die Syntax die Fehler auslöst, könnte aber die Analyse beheben….

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

1204880cookie-checkCurl in wp_remote_post umwandeln

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy