• 02-11-2023, 05:00:36
    #1
    Arkadaşlar php kod örneğim:
    <?php
    $domain = $_SERVER['SERVER_NAME'];
    
    $tags = array(1, 2);
    $data = array(
        'title' => 'Deneme Makale',
        'content' => 'Deneme yazı',
        'status' => 'publish',
        'categories' => array(1, 2),
        'tags' => $tags
    );
    $data_string = json_encode($data);
    $ch = curl_init("http://$domain/wp-json/wp/v2/posts");
    
    $headers = array(
        'Authorization: Basic ' . base64_encode('admin:123456789'),
        'Content-Type: application/json',
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        $error = curl_error($ch);
        echo 'Error: ' . $error;
    } else {
        $result = json_decode($response, true);
        print_r($result);
    }
    curl_close($ch);
    geri dönen:
    Array ( [code] => rest_cannot_create [message] => Sorry, you are not allowed to create posts as this user. [data] => Array ( [status] => 401 ) )

    htaccess:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    Edit:
    Çözüm:
    Wordpress panelden kullanıcı ayarlarında Kullanıcıya ait "Uygulama parolası" ekledim ve base64_encode('admin:123456789') bu kısmı düzenledim.

    Daha sonra ssl sorunu çıktı localde hiç bir şekilde aşılmıyordu bu sorun fakat online sunucuya taşıyıp ssl kurduktan sonra sorun kalmadı.
  • Kabul Edilen Cevap
    • 1 Beğeni
      ytscript adlı üyeden alıntı: mesajı görüntüle
      Arkadaşlar php kod örneğim:
      <?php
      $domain = $_SERVER['SERVER_NAME'];
      
      $tags = array(1, 2);
      $data = array(
          'title' => 'Deneme Makale',
          'content' => 'Deneme yazı',
          'status' => 'publish',
          'categories' => array(1, 2),
          'tags' => $tags
      );
      $data_string = json_encode($data);
      $ch = curl_init("http://$domain/wp-json/wp/v2/posts");
      
      $headers = array(
          'Authorization: Basic ' . base64_encode('admin:123456789'),
          'Content-Type: application/json',
      );
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      $response = curl_exec($ch);
      if (curl_errno($ch)) {
          $error = curl_error($ch);
          echo 'Error: ' . $error;
      } else {
          $result = json_decode($response, true);
          print_r($result);
      }
      curl_close($ch);
      geri dönen:
      Array ( [code] => rest_cannot_create [message] => Sorry, you are not allowed to create posts as this user. [data] => Array ( [status] => 401 ) )

      htaccess:
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      </IfModule>
      # END WordPress
      'Authorization: Basic ' . base64_encode('admin:123456789'), burası hatalı büyük ihtimalle adminden user alanında uygulama parolası oluşturup base64_encode('user:uygulamaparola') olarak ekleyin.
  • 02-11-2023, 06:49:10
    #2
    Bu cevap, konu sahibi tarafından kabul edilebilir bir cevap olarak işaretlendi.
    ytscript adlı üyeden alıntı: mesajı görüntüle
    Arkadaşlar php kod örneğim:
    <?php
    $domain = $_SERVER['SERVER_NAME'];
    
    $tags = array(1, 2);
    $data = array(
        'title' => 'Deneme Makale',
        'content' => 'Deneme yazı',
        'status' => 'publish',
        'categories' => array(1, 2),
        'tags' => $tags
    );
    $data_string = json_encode($data);
    $ch = curl_init("http://$domain/wp-json/wp/v2/posts");
    
    $headers = array(
        'Authorization: Basic ' . base64_encode('admin:123456789'),
        'Content-Type: application/json',
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        $error = curl_error($ch);
        echo 'Error: ' . $error;
    } else {
        $result = json_decode($response, true);
        print_r($result);
    }
    curl_close($ch);
    geri dönen:
    Array ( [code] => rest_cannot_create [message] => Sorry, you are not allowed to create posts as this user. [data] => Array ( [status] => 401 ) )

    htaccess:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    'Authorization: Basic ' . base64_encode('admin:123456789'), burası hatalı büyük ihtimalle adminden user alanında uygulama parolası oluşturup base64_encode('user:uygulamaparola') olarak ekleyin.
  • 03-11-2023, 00:08:45
    #3
    harew1 adlı üyeden alıntı: mesajı görüntüle
    'Authorization: Basic ' . base64_encode('admin:123456789'), burası hatalı büyük ihtimalle adminden user alanında uygulama parolası oluşturup base64_encode('user:uygulamaparola') olarak ekleyin.
    Evet. Dediğinizi yaptım aldığım hata bu yüzdenmiş. Fakat şimdi de "Error: SSL certificate problem: self signed certificate" hatası veriyor. localde çalışıyorum. dolayısıyla ssl kendinden sertifikalı
  • 03-11-2023, 00:23:51
    #4
    ytscript adlı üyeden alıntı: mesajı görüntüle
    Evet. Dediğinizi yaptım aldığım hata bu yüzdenmiş. Fakat şimdi de "Error: SSL certificate problem: self signed certificate" hatası veriyor. localde çalışıyorum. dolayısıyla ssl kendinden sertifikalı
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl bu satırı ekleyin ssl pasif edin.
  • 03-11-2023, 13:15:53
    #5
    hocam onu ekleyince de yine
    [CODE]
    Array
    (
     => rest_cannot_create
        [message] => Sorry, you are not allowed to create posts as this user.
        [data] => Array
            (
                [status] => 401
            )
    
    )
    bu hatayı veriyor kafayı yedim
  • 17-12-2023, 19:29:07
    #6
    Bu sorunu çözümü aslında application password oluşturmak ve şifre olarak o application passwordu kullanmak . Sonrasında Authorization headerına base64 leyip (username . ':'. pasword) şeklinde göndermek .

    Ben bunları yaptım php'den çok güzel çalışıyor . (Postman'den de çalışıyor )

    benim derdim Javasctip yazdığım bir chrome extension ile aynı API adreine aynı şekilde base64'lenmiş aynı kullanıcı adı ve şifre ile erişmeye çalıştığımda aynı API bu hatayı veriyor . Ben de çok uğraştım ama çözemiyorum.