主要内容

webread

Read content from RESTful web service

描述

example

数据= webread(url)读取来自Web服务的内容urland returns the content in数据

这web service provides a休息fulthat returns data formatted as an internet media type such as JSON, XML, image, or text.

example

数据= webread(url,QueryName1,QueryValue1,...,QueryNameN,QueryValueN)appends query parameters tourl, as specified by one or more pairs of name-value arguments. To put a query into the body of the message, useWebwrite。Web服务定义查询参数。

example

数据= webread(___,options)adds other HTTP request options, specified by theweboptionsobjectoptions。You can use this syntax with any of the input arguments of the previous syntaxes.

To return data as a specific output type, specify theContentTypeproperty ofoptions

To read content with a function, specify theContentReaderproperty ofoptions作为一个handle to the function.webread从Web服务下载数据,并使用指定功能读取数据:

  • If you specify a handle to a function that returns multiple output arguments,webreadreturns all output arguments.

  • If you specify a handle to a function that returns no output argument (such as Image Processing Toolbox™ function@implayfor video files),webreadreturns no output argument.

[数据,colormap,alpha这是给予的= webread(___)reads an image from the web service specified byurland returns the image in数据。您可以使用先前的语法仅返回图像。使用此语法返回与图像关联的colormap和alpha通道。

webreadreturns an image when the HTTP response has aContent-Typeheader field that specifies an image media type and ifimreadsupports the image format. For supported image formats, seeSupported File Formats for Import and Export

[数据,Fs这是给予的= webread(___)reads audio data from the web service specified byurland returns the audio data in数据。You can use the previous syntaxes to return the audio data only. Use this syntax to return the sample rate of the audio data in hertz.

webreadreturns audio data when the HTTP response has aContent-Typeheader field that specifies an audio media type and ifaudioreadsupports the audio format. For supported audio formats, seeSupported File Formats for Import and Export

Examples

collapse all

This example shows how to read an image from a website and display it.

读取图像数据

httpsUrl ='https://requestserver.mathworks.com'; imageUrl = strcat(httpsUrl,'/assets/computervision.jpg');rgb = webread(imageUrl); whosrgb
Name Size Bytes Class Attributes rgb 360x640x3 691200 uint8

Resize and Display Image

RGB = Imresize(RGB,0.6);IMShow(RGB)

This example shows how to read temperatures from acsv数据file.

Read Data From CSV File

httpsUrl ="https://requestserver.mathworks.com"; dataUrl = strcat(httpsUrl,"/assets/weatherStation.csv");data = webread(dataUrl);时间= [data.time];temp = [data.tempf];

Display Temperature Plot

plot(time, temp) xlabel("Time") ylabel("Temperature (Farenheit)") 标题("Temperature Over Time");axispadded

This example shows how to select a record using query parameters.

View Employee Database Structure

Display the fields of databaseemployee

httpsUrl ="https://requestserver.mathworks.com"; employeeUrl = strcat(httpsUrl,"/employee");fieldnames(webread(employeeUrl))
ans =6×1 cell{'id' } {'firstName' } {'lastName' } {'occupation'} {'age' } {'city' }

Select Employee byfirstNameandlastName

jSmith = webread(employeeUrl,"firstName","John","lastName","Smith");disp(jSmith);
ID:1名名称:'John'Lastname:'Smith'职业:“软件工程师”年龄:'32'City:'Boston'

This example shows how to return data as a specific type.

Read Data

httpUrl =“http://requestserver.mathworks.com”; employeeUrl = strcat(httpUrl,"/employee");

Return Record as Character Array

Create aweboptionsobject and set itsContentTypeto'text'。这webreadfunction converts the JSON object to a character array.

options = weboptions("ContentType","text");sbrown =韦布雷德(员工,"firstName","Sarah", options); disp(sBrown)
[{“ id”:2,“ firstName”:“ sarah”,“ lastname”:“ brown”,“ castion”:“软件工程师”,“年龄”:“ 28”,“ City”:“ New York”}这是给予的

Input Arguments

collapse all

URL to a web service, specified as a character vector or string scalar. Include the transfer protocol. Onlyhttpandhttpsare supported. The web service implements a RESTful interface. See休息fulfor more information.

例子:韦布雷德('//www.tatmou.com/matlabcentral')reads the web page and returns its HTML as a character array.

Web service query parameters, specified as one or more pairs of name-value arguments. AQueryName参数必须指定查询参数的名称,作为字符向量或字符串标量。一个QueryValueargument must be a character vector, a string scalar, or a numeric, logical, ordatetimevalue that specifies the value of the query parameter. Numeric, logical, anddatetimevalues can be in arrays. The web service defines name-value pairs that it accepts as part of a request.

什么时候you specifyQueryValue作为一个datetimeobject, you must specify itsFormat属性使其与Web服务所需的格式一致。如果是Formatproperty includes a time zone or offset, and thedatetimeobject is not zoned, thenwebreadspecifies'Local'作为时区。

什么时候QueryValuecontains multiple values in an array, you might need to specify thearrayformat财产的weboptionsobject to form-encode the array as specified by the web service.

例子:webread('//www.tatmou.com/matlabcentral/fileexchange/','term','webread')检索上传到包含单词的文件交换的文件列表webread

一个dditional HTTP request options, specified as aweboptionsobject.

You can specify theContentType财产的weboptionsobject, and pass the object as an input argument towebread。这nwebreadreturns数据as that type of output. The table lists the valid content types you can specify in aweboptionsobject.

ContentType说明符

Output Type

'auto'(default)

Output type automatically determined based on content type specified by the server.

'text'

Character vector for content types:

text/plain
text/html
text/xml
application/xml
application/javascript
application/x-javascript
应用/X-WWW-Form-urlenCoded

If a web service returns a MATLAB®file with a。m扩展,该函数将其内容返回为字符向量。

“我age'

Numeric or logical matrix forimage/format内容。如果是first output argument is an indexed image, the second output argument is the colormap, and the third output argument is the alpha channel.

For supported image formats, seeSupported File Formats for Import and Export

'audio'

Numeric matrix foraudio/formatcontent with numeric scalar sampling rate as a second output argument.

有关支持金宝app的音频格式,请参阅Supported File Formats for Import and Export

'binary'

uint8列矢量binary content (that is, content not to be treated as typechar).

'桌子'

Scalar table object for spreadsheet and CSV (text/csv) content.

'json'

char, numeric, logical, structure, or cell array, forapplication/json内容。

'xmldom'

Java®文档对象模型(DOM)节点text/xmlorapplication/xml内容。如果未指定,该函数将XML内容返回为字符向量。

'生的'

char列矢量'text','xmldom', and'json'内容。这function returns any other content type as auint8column vector.

Seeweboptionsfor all request options that areweboptionsproperties.

Output Arguments

collapse all

Content read from a web service, returned as a scalar, array, structure, or table.

Colormap associated with an indexed image, returned as a numeric array.

与索引图像关联的Alpha通道,作为数字阵列返回。

Sample rate of audio data in hertz, returned as a positive numeric scalar.

More About

collapse all

休息ful

休息方法代表性国家转移, a common architectural style for web services. RESTful interfaces provide standard HTTP methods such as GET, PUT, POST, or DELETE.

提示

  • For functionality not supported by the RESTful web services functions, see theUse HTTP with MATLAB

  • webreadsupports HTTP GET and POST methods. Many web services provide both GET and POST methods to request data. To send an HTTP POST request, specify theRequestMethodproperty ofoptionsas'post'。However,webread将查询选项放入url, not in the body of the request message. To put a query into the body, useWebwrite

  • For HTTP POST requests, thewebreadfunction supports only the应用/X-WWW-Form-urlenCodedmedia type. To send a POST request with content of any other internet media type, useWebwrite

  • This function does not examine the document contents to determine how to process it. For example, HTML and XML documents often contain atag that specifies the document character encoding. If the encoding is different from the defaultwebreadencoding, then specify the correctCharacterEncodingoption inweboptions

  • To specify proxy server settings, seeProxy Server Authentication

H版istory

Introduced in R2014b