How to post tweets on twitter with php for specific countries (Tweet Delivery By Country 'Geo-targeting')

php

#1

I am working on twitter api, and I am successfully posting the tweets on twitter with following code.

<?php

$config['CONSUMER_KEY']         =   'XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$config['CONSUMER_SECRET']      =   'XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$config['ACCESS_TOKEN']         =   'XXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$config['ACCESS_TOKEN_SECRET']  =   'XXXXXXXXXXXXXXXXXXXXXXXXXXXX';

$connection         =   new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $OAUTH_TOKEN, $OAUTH_TOKEN_SECRET);
$content            =   $connection->get('account/verify_credentials');

$my_tweets_post     =   $connection->post('statuses/update',
                        array(
                        'status' => 'This is my tweet text'
                    ));

 ?>

but now i want to show the tweets which I have posted for specific country’s users like Canada. I have also seen the following link to post tweets for specific countries (Geo Targeting), but I am not getting that how to use parameters to tweet delivery by country.

Here is the link: https://dev.twitter.com/ads/campaigns/geo-casting

Please help me.

Thanks in advance.


#2

(moving to the Ads API category)


#3

In order to use the Tweet Delivery by Country feature, your app must be whitelisted for it based on your partnership with Twitter. It’s important to note that this feature limits organic Tweet delivery and has no effect when actually promoting the Tweet as part of a campaign in the Twitter Ads Platform.

If you’ve been whitelisted, to target only specific countries when you create a Tweet, simply pass one additional parameter, narrowcast_place_ids. The param takes a list of place IDs, as exposed on the GET geo/search endpoint, or if you have access to the Ads API, using the GET targeting_criteria/locations endpoint.

So, from the docs you referred to, in order to target your Tweet to only users in Canada, pass in the place ID as an additional param with value narrowcast_place_ids=3376992a082d67c7 (in addition to your status).

You can check to see if one of your Tweets had limited organic delivery by checking the scopes attribute on the Tweet object:

 "scopes": {
    "place_ids": ["3376992a082d67c7"]
  }

If your Tweet is not limited in delivery, it will not have the scopes attribute returned. If the Tweet has been limited to Promoted-only display, the scope will be set to "followers": false:

"scopes": {
    "followers": false
  }

#5

My app is whitelisted,
but please tell how to pass additional param narrowcast_place_ids in my following code, because i am using the following code to post tweets on twitter with php

$connection->post(‘statuses/update’,array(‘status’ => ‘This is tweet text’ ));


#6

I have not tried this in PHP, but I would assume it would be something like this instead, appending additional parameters to your array… (to narrowcast to Canada as @jaakkosf described above)

$connection->post('statuses/update',array('status' => 'This is tweet text', 'narrowcast_place_ids' => '3376992a082d67c7'));

#7

no not working


#8

I am having no trouble using this parameter with the statuses/update endpoint. If you’re having trouble, we’ll need more from you for anyone to be able to assist – code that you’re using to post the Tweet and what the response you’re getting from the Twitter API looks like.

Here’s the code I just ran that was successful in posting this.

<?php

$consumer_key = 'consumer_key';
$consumer_secret = 'consumer_secret';
$access_token = 'access_token';
$access_token_secret = 'access_token_secret';

require "twitteroauth/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);

$statuses = $connection->post("statuses/update", ["status" => "narrowcasted php test - canada", "narrowcast_place_ids" => "3376992a082d67c7"]);

var_dump($statuses);

exit(0);
?>

You’ll know it was successful if you see the scopes attribute on the resulting Tweet object:

"scopes": {
    "place_ids": ["3376992a082d67c7"]
  }

Let us know if that sorts this out for you.


#9

its returning me this error

object(stdClass)#67 (1) { [“errors”]=> array(1) { [0]=> object(stdClass)#68 (2) { [“code”]=> int(37) [“message”]=> string(36) “Not authorized to use this endpoint.” } } }


#10

Is that parameter documented?


#11

yes, when i am using narrowcast_place_ids parameter in array,

its returning me this error

object(stdClass)#67 (1) { [“errors”]=> array(1) { [0]=> object(stdClass)#68 (2) { [“code”]=> int(37) [“message”]=> string(36) “Not authorized to use this endpoint.” } } }


#12

Your Twitter application must be whitelisted for the Ads API in order to utilize that param. If your app can access the Ads API and you can’t use this endpoint, reach out to the contact you have from the Twitter Program Management team and they can sort that out for you.


#13

Currently only documented on our Tweet Delivery by Country page. Improvements to that are in the works, however…