Php http client4/16/2023 ![]() 3xx,Ĥxx or 5xx), the getHeaders(), getContent() and toArray() methods When the HTTP status code of the response is in the 300-599 range (i.e. Exceptions implementing the DecodingExceptionInterfaceĪre thrown when a content-type cannot be decoded to the expected representation.Exceptions implementing the TransportExceptionInterfaceĪre thrown when a lower level issue occurs.Exceptions implementing the HttpExceptionInterfaceĪre thrown when your code does not handle the status codes in the 300-599 range.There are three types of exceptions, all of which implement the returns detailed logs about the requests and responses of the HTTP transaction $httpLogs = $response ->getInfo( 'debug') this returns the final response URL (resolving redirections if needed) $url = $response ->getInfo( 'url') you can get individual info too $startTime = $response ->getInfo( 'start_time') returns info coming from the transport layer, such as "response_headers", // "redirect_count", "start_time", "redirect_url", etc. cancels the request/response $response ->cancel() casts the response content to a PHP stream resource $content = $response ->toStream() casts the response JSON content to a PHP array $content = $response ->toArray() gets the response body as a string $content = $response ->getContent() gets the HTTP headers as string with the header names lower-cased $headers = $response ->getHeaders() Symfony\Contracts\HttpClient\HttpClientInterface $githubClientĪs the type and name of an argument, autowiring will inject the github.clientģ1 $response = $client ->request( 'GET', ' // gets the HTTP status code of the response $statusCode = $response ->getStatusCode() Methods defined by Symfony to choose a specific service.Įach client has a unique service named after its configuration.Įach scoped client also defines a corresponding named autowiring alias. If you use scoped clients in the Symfony framework, you must use any of the Requested URL matches one of the regular expressions set by the scope option. You can define several scopes, so that each set of options is added only if a relative URLs will use the 2nd argument as base URI and use the options of the 3rd argument $client = ScopingHttpClient ::forBaseUri( $client, '', [ the options defined as values apply only to the URLs matching // the regular expressions defined as keys ' => [ $client = new ScopingHttpClient( $client, [ Use Symfony\ Component\ HttpClient\ ScopingHttpClient DifferentĪuthentication strategies can be added by developers.30 application/3 json token %env(GITHUB_API_TOKEN)% application/3 json token %env(GITHUB_API_TOKEN)% Ģ3 use Symfony\ Component\ HttpClient\ HttpClient They can also be used when constructing Client to createĬake\Http\Client supports a few different authentication systems. The options parameter is always the 3rd parameter in each of the HTTP methods. Redirect - Number of redirects to follow. Requires $data toĮither be a string, or the _content option to be set when doing GET Type - Send a request body in a custom content type. Timeout - Duration to wait before timing out in seconds. Ssl_cafile - defaults to built in cafile. Validate the SSL certificate against the host name. Host name verification when verifying SSL certificates (not recommended). ![]() Set to false to disable SSL certification The following keys can be used in $options:Īuth - Array of authentication data, the type key is used to delegate toĪn authentication strategy. $http = new Client () $response = $http -> get ( '', , ) Request Method Options ¶Įach HTTP method takes an $options parameter which is used to provideĪddition request information. Send a JSON body in a GET request with query string parameters.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |