Main Content

getdata

RetrieveTwitterdata

Description

example

d= getdata(c,baseurl)retrieves Twitter®data for REST API GET endpoints that do not require any web service query parameters.

example

d= getdata(c,baseurl,parameters)retrieves Twitter data using web service query parameters. The Twitter REST API defines web service query parameters for each endpoint. For valid parameters, see theTwitterREST API Endpoint Reference Documentation.

example

d= getdata(c,baseurl,QueryName1,QueryValue1,...,QueryNameN,QueryValueN)specifies web service query parameters as one or more pairs of name-value arguments.

Examples

collapse all

Use a Twitter connection object to return locations for trending topics. The REST API endpointGET trends/availabledoes not require any web service query parameters.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey ='abcdefghijklmnop123456789'; consumersecret ='qrstuvwxyz123456789'; accesstoken ='123456789abcdefghijklmnop'; accesstokensecret ='123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If theStatusCodeproperty has the valueOK, the connection is successful.

c.StatusCode
ans = OK

Specify the Twitter base URL.

baseurl ='https://api.twitter.com/1.1/trends/available.json';

Retrieve locations for trending topics using the Twitter connection object and the base URL.

d= getdata(c,baseurl)
d= ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0

dis amatlab.net.http.ResponseMessageobject. TheStatusCodeproperty showsOK, indicating a successful HTTP request.

Access the location data. Display the structureData.

d.Body.Data
ans = 467×1 struct array with fields: name placeType url parentid country woeid countryCode

的结构reDatais a structure array with the fieldname, which contains the name of a location for a trending topic.

Access the first location.

d.Body.Data(1).name
ans = 'Worldwide'

You can retrieve data for other REST API endpoints by substituting another URL for thebaseurlinput argument. Or, you can search for Tweets using thesearchfunction.

Use a Twitter connection object to retrieve follower information. Specify the count of followers as a structure.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey ='abcdefghijklmnop123456789'; consumersecret ='qrstuvwxyz123456789'; accesstoken ='123456789abcdefghijklmnop'; accesstokensecret ='123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If theStatusCodeproperty has the valueOK, the connection is successful.

c.StatusCode
ans = OK

Set the Twitter base URL to access theGET followers/listREST API endpoint. Specify one follower by defining the structureparameterswith the field set to 1. Search for one follower of the current account using the Twitter connection object, base URL, and structureparameters.

baseurl ='https://api.twitter.com/1.1/followers/list.json'; parameters.count = 1; d = getdata(c,baseurl,parameters)
d= ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0

dis amatlab.net.http.ResponseMessageobject. TheStatusCodeproperty showsOK, indicating a successful HTTP request.

Access information about the follower.

d.Body.Data.users
ans = struct with fields: id: 12345678 id_str: '12345678' name: 'Full Name' ...

d.Body.Data.usersis a structure that has a field for each piece of account information. For example, the first three fields are:

  • Account identifier as a number

  • Account identifier as a character vector

  • Full name of the account as a character vector

(These values do not represent real Twitter data.)

You can retrieve data for other REST API endpoints by substituting another URL for thebaseurlinput argument. Or, you can search for Tweets using thesearchfunction.

Use a Twitter connection object to retrieve follower information. Specify the count of followers as a name-value argument.

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey ='abcdefghijklmnop123456789'; consumersecret ='qrstuvwxyz123456789'; accesstoken ='123456789abcdefghijklmnop'; accesstokensecret ='123456789qrstuvwxyz'; c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If theStatusCodeproperty has the valueOK, the connection is successful.

c.StatusCode
ans = OK

Set the Twitter base URL to access theGET followers/listREST API endpoint. Search for one follower of the current account using the Twitter connection object, base URL, and name-value argumentcount.

baseurl ='https://api.twitter.com/1.1/followers/list.json'; d = getdata(c,baseurl,'count',1)
d= ResponseMessage with properties: StatusLine: 'HTTP/1.1 200 OK' StatusCode: OK Header: [1×25 matlab.net.http.HeaderField] Body: [1×1 matlab.net.http.MessageBody] Completed: 0

dis amatlab.net.http.ResponseMessageobject. TheStatusCodeproperty showsOK, indicating a successful HTTP request.

Access information about the follower.

d.Body.Data.users
ans = struct with fields: id: 12345678 id_str: '12345678' name: 'Full Name' ...

d.Body.Data.usersis a structure that has a field for each piece of account information. For example, the first three fields are:

  • Account identifier as a number

  • Account identifier as a character vector

  • Full name of the account as a character vector

(These values do not represent real Twitter data.)

You can retrieve data for other REST API endpoints by substituting another URL for thebaseurlinput argument. Or, you can search for Tweets using thesearchfunction.

Input Arguments

collapse all

Twitter connection, specified as atwitterobject.

Twitter base URL, specified as a character vector or string scalar. Use this URL to access the Twitter REST API endpoints.

Example:'https://api.twitter.com/1.1/followers/list.json'specifies a GET REST API endpoint.

Data Types:char|string

Web service query parameters, specified as a structure. Each parameter is specified as a field in the structure. Set the field to a specific value in the structure. For example, specify the number of items for the HTTP request:

parameters.count = 20;

The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request. For valid parameters, see theTwitterREST API Endpoint Reference Documentation.

Data Types:struct

Web service query parameters, specified as one or more pairs of name-value arguments. AQueryNameargument is a character vector or string scalar that specifies the name of a query parameter. AQueryValueargument is a character vector or string scalar that specifies the value of the query parameter.

The Twitter REST API defines web service query parameters that it accepts as part of an HTTP request. For valid parameters, see theTwitterREST API Endpoint Reference Documentation.

Example:'count',20specifies the number of items for the HTTP request.

Data Types:char|string

Output Arguments

collapse all

Twitter数据,作为一个返回matlab.net.http.ResponseMessageobject.

To retrieve Twitter data, access properties ind, for example:

data = d.Body.Data
data = struct with fields: statuses: {50×1 cell} search_metadata: [1×1 struct]

Continue to access the nested structuredatato retrieve Twitter data. For accessing nested structures, seeAccess Data in Nested Structures.

Limitations

Version History

Introduced in R2017b