skip to Main Content
<?php

// Set username and password
$username = 'XXX';
$password = 'XXX';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = 'https://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'https://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'failure';
} else {
    echo 'success';
}

echo $buffer;

?>

Hallo, I have an Problem with the Code above. I have it from an Website. It returns at $buffer, that the Server understood the request but is an forbidden one.

2

Answers


  1. The Twitter API does not support Tweeting with a username and password, you need to use OAuth. Also, the Twitter API URL that you are using in this code is very old (like, about 14 years old). You need to use the official Twitter API. There are some current PHP libraries available.

    Login or Signup to reply.
  2. It’s probably disabled within your server’s configuration file. You should look into your specific server settings and fiddle around, curl_exec might be disabled by default.

    You can check if the setting is enabled using the following command;

    <?php phpinfo(); ?>
    

    If you need help with configuring the php.ini file, please check this topic; enable curl_exec on php.ini. You would have to look for a line with disabled_functions, curl_exec might be added there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search