Loyafis

Uzman
Katılım
11 Ekim 2024
Mesajlar
11
Beğeniler
3
Merhaba, yardımcı olursanız çok mutlu olurum. Twitter'da bir bot yapmaya çalışıyorum fakat başarılı olamıyorum. Twitter API'leri falan her şey mevcut, ancak haber sitesinden haberleri çekip paylaşmasını istiyorum.

Bu işlemi yaparken çok fazla hata alıyorum. Bu konu hakkında bilgisi olan veya hazır botu olan varsa yardımcı olursa çok sevinirim.
 
PHP ile yapabilirsin bu durumu, cron yapıp sürekli veriyi kontrol ettirebilirsin veya python ile bu işlemi yapabilirsin. öncelikle ilgili siteden veriyi alman için servisin olması lazım örneğin rss ile veriyorsa onunla alabilirsin veya PHP ile ilgili siteyi parçalayıp önce URL adreslerini toplatabilir daha sonra o URL içerisinde ilgili haberin ilgili alanını aldırıp onunla paylaştırabilirsin.

Guzzle ve TwitterOAuth'ı composer ile çektir. aşağıdaki alana gerekli doğrulama kodlarını ekle ve cron oluştur. otomatik işlemi yapar.

Kod:
<?php

require 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
use GuzzleHttp\Client;

$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';
$accessToken = 'YOUR_ACCESS_TOKEN';
$accessTokenSecret = 'YOUR_ACCESS_TOKEN_SECRET';

$rssUrl = "https://www.ntv.com.tr/gundem.rss";
$lastPostedFile = 'last_posted.txt';

$client = new Client();
$response = $client->get($rssUrl);
$xml = simplexml_load_string($response->getBody()->getContents());

$entry = $xml->entry[0];
$link = (string)$entry->link['href'];
$title = (string)$entry->title;

if (file_exists($lastPostedFile)) {
    $lastPosted = file_get_contents($lastPostedFile);
    if ($lastPosted === $link) {
        exit;
    }
}

try {
    $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $status = $title . ' ' . $link;
    $twitter->post('statuses/update', ['status' => $status]);
    file_put_contents($lastPostedFile, $link);
} catch (Exception $e) {
    echo $e->getMessage();
}
 
Son düzenleme:
PHP ile yapabilirsin bu durumu, cron yapıp sürekli veriyi kontrol ettirebilirsin veya python ile bu işlemi yapabilirsin. öncelikle ilgili siteden veriyi alman için servisin olması lazım örneğin rss ile veriyorsa onunla alabilirsin veya PHP ile ilgili siteyi parçalayıp önce URL adreslerini toplatabilir daha sonra o URL içerisinde ilgili haberin ilgili alanını aldırıp onunla paylaştırabilirsin.

Guzzle ve TwitterOAuth'ı composer ile çektir. aşağıdaki alana gerekli doğrulama kodlarını ekle ve cron oluştur. otomatik işlemi yapar.

Kod:
<?php

require 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
use GuzzleHttp\Client;

$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';
$accessToken = 'YOUR_ACCESS_TOKEN';
$accessTokenSecret = 'YOUR_ACCESS_TOKEN_SECRET';

$rssUrl = "https://www.ntv.com.tr/gundem.rss";
$lastPostedFile = 'last_posted.txt';

$client = new Client();
$response = $client->get($rssUrl);
$xml = simplexml_load_string($response->getBody()->getContents());

$entry = $xml->entry[0];
$link = (string)$entry->link['href'];
$title = (string)$entry->title;

if (file_exists($lastPostedFile)) {
    $lastPosted = file_get_contents($lastPostedFile);
    if ($lastPosted === $link) {
        exit;
    }
}

try {
    $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $status = $title . ' ' . $link;
    $twitter->post('statuses/update', ['status' => $status]);
    file_put_contents($lastPostedFile, $link);
} catch (Exception $e) {
    echo $e->getMessage();
}
Şimdi ben kodu aldım, API'leri doldurdum, RSS'yi doldurdum, çalıştırdım ama 'bana gönderilmedi' hatası aldım (gerekli kütüphaneleri kurdum). Böyle bir hata da aldım: NULL. HTTP Kod: 404. Tweet gönderilirken hata oluştu. Bilinmeyen bir hata oluştu.

Yardımcı olursan çok sevinirim !

PHP:
<?php

require 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
use GuzzleHttp\Client;

// Hata raporlamayı etkinleştir
error_reporting(E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1);

// Twitter API anahtarları
$consumerKey = 'api girelecek yer';
$consumerSecret = 'api girelecek yer';
$accessToken = 'api girelecek yer';
$accessTokenSecret = 'api girelecek yer';

// RSS URL'si
$rssUrl = "https://www.aspor.com.tr/rss";
$lastPostedFile = 'last_posted.txt';

$client = new Client(['verify' => false]); // HTTPS sertifikası kontrolünü devre dışı bırak (güvenli değil, sadece test için)
$response = $client->get($rssUrl);
$xml = simplexml_load_string($response->getBody()->getContents());

$entry = $xml->channel->item[0];
$link = (string)$entry->link;
$title = (string)$entry->title;

// Son paylaşılan haberi kontrol et
if (file_exists($lastPostedFile)) {
    $lastPosted = file_get_contents($lastPostedFile);
    if ($lastPosted === $link) {
        exit("Zaten paylaşıldı: $title\n");
    }
}

try {
    // Twitter'da paylaş
    $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $status = $title . ' ' . $link;
    $result = $twitter->post('statuses/update', ['status' => $status]);

    if ($twitter->getLastHttpCode() == 200) {
        echo "Başarıyla paylaşıldı: $title\n";
        file_put_contents($lastPostedFile, $link);
    } else {
        echo "Paylaşım başarısız oldu.\n";
        print_r($result); // API yanıtını yazdır
    }
} catch (Exception $e) {
    echo "Hata: " . $e->getMessage() . "\n";
}

PHP ile yapabilirsin bu durumu, cron yapıp sürekli veriyi kontrol ettirebilirsin veya python ile bu işlemi yapabilirsin. öncelikle ilgili siteden veriyi alman için servisin olması lazım örneğin rss ile veriyorsa onunla alabilirsin veya PHP ile ilgili siteyi parçalayıp önce URL adreslerini toplatabilir daha sonra o URL içerisinde ilgili haberin ilgili alanını aldırıp onunla paylaştırabilirsin.

Guzzle ve TwitterOAuth'ı composer ile çektir. aşağıdaki alana gerekli doğrulama kodlarını ekle ve cron oluştur. otomatik işlemi yapar.

Kod:
<?php

require 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
use GuzzleHttp\Client;

$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';
$accessToken = 'YOUR_ACCESS_TOKEN';
$accessTokenSecret = 'YOUR_ACCESS_TOKEN_SECRET';

$rssUrl = "https://www.ntv.com.tr/gundem.rss";
$lastPostedFile = 'last_posted.txt';

$client = new Client();
$response = $client->get($rssUrl);
$xml = simplexml_load_string($response->getBody()->getContents());

$entry = $xml->entry[0];
$link = (string)$entry->link['href'];
$title = (string)$entry->title;

if (file_exists($lastPostedFile)) {
    $lastPosted = file_get_contents($lastPostedFile);
    if ($lastPosted === $link) {
        exit;
    }
}

try {
    $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
    $status = $title . ' ' . $link;
    $twitter->post('statuses/update', ['status' => $status]);
    file_put_contents($lastPostedFile, $link);
} catch (Exception $e) {
    echo $e->getMessage();
}
Kendim sıfırdan bir kod yazdım ve çalıştı 60 saniyede bir RSS'yi kontrol ediyor ve paylaşıyor yeni haber gelince teşekkür ederim yardımcı olduğun için.
 
Son düzenleme: