PHP WEB SERVICES
By : Abdel Rahman Karim
COURSE MAIN CONTENTS
 1- Introduction
 What’s a webservice?
 PHP and JSON
 2- Connect to Remote WEB SERVICES
 CURL
 Composer
 GUZZLE
 Open weather
 TEXT LOCAL SMS Gateway
 Using multiple APIs concurrently
 Calling an API Async
 3-
 3- Create REST API
 HTTP in details
 REST APIs Guidelines
 Create a simple REST API
 Documenting REST API by APIDOC
 4- Create SOAP API
 PHP & XML
 SOAP
 WSDL
 SOAP & REST Comparison
 Cosnuming SOAP
 Creating SOAP
 Generating WSDL by php2wsdl
PART1 : INTRODUCTION
WHAT IS THE DIFFERENCE ??
Classic request response Web Services
Web services definition
 a Web service is a software application with a
standardized way of providing interoperability
between applications. It does so over HTTP using
technologies such as SOAP, REST
 Webservices = Data Transfer method using
certain protocol (such as http) and data formats
(such as JSON Or XML)
 API and web services are used as alternative to
each other .
Most Famous type :
 SOAP
 REST
BUZZWORDS
End Point
 The endpoint is the URL where your
service can be accessed by a client
application. The same web service can
have multiple endpoints.
SDK
 an SDK is implementation tooling. It's
like a kit that allows** you to build
something custom to hook up to the
telephone system or electrical wiring.
TYPES OF WEB SERVICES
BONUS QUESTIONS
 Why web services sent JSON or XML but not HTML ?
 Can you think of 3 web services ?
 Would you use GET or POST to communicate with an API ?
 Imagine what you need to know from a 3rd party API to communicate
with it ?
WEBSERVICES
 Open weather for weather
 Textlocal for SMS sending
 HyperPay for payment
 And so many more
JSON & PHP
JSON
 JSON stands for JavaScript Object Notation.
JSON is a standard lightweight data-
interchange format which is quick and easy to
parse and generate.
 JSON, like XML, is a text-based format that's
easy to write and easy to understand for both
humans and computers
 JSON is based on two basic structures:
 Object: This is defined as a collection of
key/value pairs (i.e. key:value). Each object
begins with a left curly bracket { and ends with
a right curly bracket }. Multiple key/value pairs
are separated by a comma ,.
 Array: This is defined as an ordered list of
values. An array begins with a left bracket [
and ends with a right bracket ]. Values are
separated by a comma ,.
JSON STRUCTURE
JSON Object JSON Array
HOW CAN YOU DESCRIBE THIS FILE ?
ENCODING
Convert array to JSON
(Encoding)
ENCODE 2
DECODING AS OBJECT
DECODING AS ASSOCIATIVE ARRAY
WILL NEED IT TO READ FROM JSON
FILE?
IF THE SCRIPT SHOULD OUTPUT JSON
NOT HTML
WHAT IS THE STRONG POINT OF JSON
OVER XML ?
PART 2 : CONNECTING TO REMOTE
WEBSERVICES
USING AN API GUIDELINES
 Each webservice will have a certain URL you call to retrieve and post
data
 Before using any 3rd party webservice, you subscribe and they send you
an API hash key on email to use it in all your requests so they know the
request comes from you .
 Each API will have a documentation and example
USING AN EXTERNAL API
1- CURL
2- GUZZLE (third
party)
HTTP COMMAND LINE
CURL IS A COMMAND LINE TOOL FOR CREATING ANY WEB REQUEST IMAGINABLE
GET
 Curl http://www.google.com
 Curl –v http://www.google.com
 -v shows every thing including headers
 curl –V is different than curl -v
POST
 Curl –X POST {{url}}
 Curl –v –X POST {{URL}}
 Curl –v –x POST {{URL}} -d
CURL EXAMPLES
CURL WITH PHP
THINK AS PHP, WHAT FUNCTION DO YOU NEED ?
CURL SEQUENCE
Sequence
1. Open connection, return handler
2. Use handler to add options
3. Execute request, get result
4. Close connection
GET POST
 ALL OPTIONS START WITH :
CURLOPT_
 CURL_OPT_RETURNTRANSFER :
Returns the result not output it
 Notice the option sometimes you
need to give the user who will use
some functions to return or echo
output, Think of example ..
EXAMPLE OPEN WEATHER
API
Before submit After Submit
EXAMPLE OPENWEATHER API TO GET
THE WEATHER FOR ANY CITY
Steps to use the API
 Create an account in
https://openweathermap.org
 Click API tab and subscribe in the current
weather API
 Get the API key by email
 Wait till your API key is activated
 Read the API documentation
 Get the city list JSON file , if not find it here :
http://cutescripts.net/api/city.list.json
API KEY INSTEAD OF SENDING THE ACTUAL
USER CREDENTIALS IN REQUEST
TEST WITH POSTMAN FIRST
OPEN WEATHER API ( NOT OOP  )
Code to call the API
NOW SAME EXAMPLE IN OOP
SENDING SMS MESSAGE VIA
HTTPS://WWW.TEXTLOCAL.COM
SMS IN OOP.
PHP CONFIGURATION WILL BE NEEDED
 Php.ini
 Ini_get
 Ini_set
 Phpinfo()
 Extension_loaded()
ERROR REPORTING
 // Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
 // Report all errors
error_reporting(E_ALL);
 // Turn off error reporting
error_reporting(0);
If(__DEBUG_MODE_ === 1)
error_reporting(E_ALL);
else
error_reporting(E_ERROR | E_PARSE );
EXCEPTION HANDLING
 Exception handling is the process of
responding to exceptions by gathering
info about it, prevent the system from
crashing and customize how the error
will be logged and how the message
would be communicated to the user
SPECIAL TYPE OF EXCEPTION
DEFAULT EXCEPTION HANDLING
REVISION QUIZ
 1) is a webservice type
A. RESTful
B. WSDL
C. Endpoint
D. SDK
 2) You can force JSON Object to be
always decoded to PHP array
A. True
B. False
REVISION QUIZ
 3) --------------- Returns a handle
A. curl_setopt
B. curl_exec
C. curl_init
D. Curl_open
 4) curl_exec()
A. Returns result
B. Returns or output results
C. Output results
D. None of the above
5) An API key or hash key usually is
A. Differ Per User and it’s used for
authentication
B. Same for any user as long as they use
the same API
C. Differ by the type of web services
soap & Rest
6) When dealing with a complicated API
it’s logic to search for an available ______
for it
1. End point
2. Post man
3. SDK
4. Web service
7) What is the missing line of code
 7)
8) When reading a JSON file, it’s better to use file_get_content() function because :
A. It returns the file content as string
B. It returns all the file content no matter how large
C. It can read a remote file content, if it’s url is passed
D. It returns the file content as array, each element is a line
E. A & B
F. A & B & C
G. All the above
9) To change any
configuration of php for a
single script
A. Update php.ini file
B. Ini_set
C. Ini_get
D. All the above
10) To know if an extension
is loaded
a. Phpinfo
b. Extension_loaded()
c. Ini_set
d. Update php.ini
e. A & b
f. All the above

Day01 api

  • 1.
    PHP WEB SERVICES By: Abdel Rahman Karim
  • 2.
    COURSE MAIN CONTENTS 1- Introduction  What’s a webservice?  PHP and JSON  2- Connect to Remote WEB SERVICES  CURL  Composer  GUZZLE  Open weather  TEXT LOCAL SMS Gateway  Using multiple APIs concurrently  Calling an API Async  3-  3- Create REST API  HTTP in details  REST APIs Guidelines  Create a simple REST API  Documenting REST API by APIDOC  4- Create SOAP API  PHP & XML  SOAP  WSDL  SOAP & REST Comparison  Cosnuming SOAP  Creating SOAP  Generating WSDL by php2wsdl
  • 3.
  • 4.
    WHAT IS THEDIFFERENCE ?? Classic request response Web Services
  • 5.
    Web services definition a Web service is a software application with a standardized way of providing interoperability between applications. It does so over HTTP using technologies such as SOAP, REST  Webservices = Data Transfer method using certain protocol (such as http) and data formats (such as JSON Or XML)  API and web services are used as alternative to each other . Most Famous type :  SOAP  REST
  • 6.
    BUZZWORDS End Point  Theendpoint is the URL where your service can be accessed by a client application. The same web service can have multiple endpoints. SDK  an SDK is implementation tooling. It's like a kit that allows** you to build something custom to hook up to the telephone system or electrical wiring.
  • 7.
    TYPES OF WEBSERVICES
  • 8.
    BONUS QUESTIONS  Whyweb services sent JSON or XML but not HTML ?  Can you think of 3 web services ?  Would you use GET or POST to communicate with an API ?  Imagine what you need to know from a 3rd party API to communicate with it ?
  • 9.
    WEBSERVICES  Open weatherfor weather  Textlocal for SMS sending  HyperPay for payment  And so many more
  • 10.
    JSON & PHP JSON JSON stands for JavaScript Object Notation. JSON is a standard lightweight data- interchange format which is quick and easy to parse and generate.  JSON, like XML, is a text-based format that's easy to write and easy to understand for both humans and computers  JSON is based on two basic structures:  Object: This is defined as a collection of key/value pairs (i.e. key:value). Each object begins with a left curly bracket { and ends with a right curly bracket }. Multiple key/value pairs are separated by a comma ,.  Array: This is defined as an ordered list of values. An array begins with a left bracket [ and ends with a right bracket ]. Values are separated by a comma ,.
  • 11.
  • 12.
    HOW CAN YOUDESCRIBE THIS FILE ?
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
    WILL NEED ITTO READ FROM JSON FILE?
  • 18.
    IF THE SCRIPTSHOULD OUTPUT JSON NOT HTML
  • 19.
    WHAT IS THESTRONG POINT OF JSON OVER XML ?
  • 20.
    PART 2 :CONNECTING TO REMOTE WEBSERVICES
  • 21.
    USING AN APIGUIDELINES  Each webservice will have a certain URL you call to retrieve and post data  Before using any 3rd party webservice, you subscribe and they send you an API hash key on email to use it in all your requests so they know the request comes from you .  Each API will have a documentation and example
  • 22.
    USING AN EXTERNALAPI 1- CURL 2- GUZZLE (third party)
  • 23.
    HTTP COMMAND LINE CURLIS A COMMAND LINE TOOL FOR CREATING ANY WEB REQUEST IMAGINABLE GET  Curl http://www.google.com  Curl –v http://www.google.com  -v shows every thing including headers  curl –V is different than curl -v POST  Curl –X POST {{url}}  Curl –v –X POST {{URL}}  Curl –v –x POST {{URL}} -d
  • 24.
  • 25.
    CURL WITH PHP THINKAS PHP, WHAT FUNCTION DO YOU NEED ?
  • 26.
    CURL SEQUENCE Sequence 1. Openconnection, return handler 2. Use handler to add options 3. Execute request, get result 4. Close connection
  • 27.
    GET POST  ALLOPTIONS START WITH : CURLOPT_  CURL_OPT_RETURNTRANSFER : Returns the result not output it  Notice the option sometimes you need to give the user who will use some functions to return or echo output, Think of example ..
  • 28.
  • 29.
    EXAMPLE OPENWEATHER APITO GET THE WEATHER FOR ANY CITY Steps to use the API  Create an account in https://openweathermap.org  Click API tab and subscribe in the current weather API  Get the API key by email  Wait till your API key is activated  Read the API documentation  Get the city list JSON file , if not find it here : http://cutescripts.net/api/city.list.json
  • 30.
    API KEY INSTEADOF SENDING THE ACTUAL USER CREDENTIALS IN REQUEST
  • 31.
  • 32.
    OPEN WEATHER API( NOT OOP  ) Code to call the API
  • 33.
  • 34.
    SENDING SMS MESSAGEVIA HTTPS://WWW.TEXTLOCAL.COM
  • 35.
  • 36.
    PHP CONFIGURATION WILLBE NEEDED  Php.ini  Ini_get  Ini_set  Phpinfo()  Extension_loaded()
  • 37.
    ERROR REPORTING  //Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE);  // Report all errors error_reporting(E_ALL);  // Turn off error reporting error_reporting(0); If(__DEBUG_MODE_ === 1) error_reporting(E_ALL); else error_reporting(E_ERROR | E_PARSE );
  • 38.
    EXCEPTION HANDLING  Exceptionhandling is the process of responding to exceptions by gathering info about it, prevent the system from crashing and customize how the error will be logged and how the message would be communicated to the user
  • 39.
    SPECIAL TYPE OFEXCEPTION
  • 40.
  • 41.
    REVISION QUIZ  1)is a webservice type A. RESTful B. WSDL C. Endpoint D. SDK  2) You can force JSON Object to be always decoded to PHP array A. True B. False
  • 42.
    REVISION QUIZ  3)--------------- Returns a handle A. curl_setopt B. curl_exec C. curl_init D. Curl_open  4) curl_exec() A. Returns result B. Returns or output results C. Output results D. None of the above
  • 43.
    5) An APIkey or hash key usually is A. Differ Per User and it’s used for authentication B. Same for any user as long as they use the same API C. Differ by the type of web services soap & Rest 6) When dealing with a complicated API it’s logic to search for an available ______ for it 1. End point 2. Post man 3. SDK 4. Web service
  • 44.
    7) What isthe missing line of code  7)
  • 45.
    8) When readinga JSON file, it’s better to use file_get_content() function because : A. It returns the file content as string B. It returns all the file content no matter how large C. It can read a remote file content, if it’s url is passed D. It returns the file content as array, each element is a line E. A & B F. A & B & C G. All the above
  • 46.
    9) To changeany configuration of php for a single script A. Update php.ini file B. Ini_set C. Ini_get D. All the above 10) To know if an extension is loaded a. Phpinfo b. Extension_loaded() c. Ini_set d. Update php.ini e. A & b f. All the above