However, before we can separate the components of the response, we need to get the size of the header. You can use Curl to make all kinds of HTTP requests, including sending custom headers, sending body data, and using different verbs to make your request. Using this method, we can return both the body and header part of the response. Next, we will need to cut out the headers. Doing this will include the headers in the response downloaded by cURL. As of cURL 7.10. This can be utilizable if you optate to set custom request headers when performing a HTTP request through cURL in PHP. Using CURLOPTHEADER option With the curlsetopt () method, when CURLOPTHEADER is set to true, curlexec will output response header. option This may be one of the following constants: CURLINFOEFFECTIVEURL - Last effective URL CURLINFOHTTPCODE - The last response code. There are two ways to get response headres from PHP cURL. To do this, we first determine the size of the response header, and then simply cut it from the response using the substr() function.įirst, we set the CURLOPT_HEADER option true. handle A cURL handle returned by curlinit (). However, we can still "cut" them from the full response. There is no build-in way to only return the response headers using cURL in PHP. Convert the $headers string to an indexed array $headers_indexed_arr = explode ( " \r\n ", $headers ) // Define as array before using in loop $headers_arr = array () // Remember the status message in a separate variable $status_message = array_shift ( $headers_indexed_arr ) // Create an associative array containing the response headers foreach ( $headers_indexed_arr as $value ) // Show that it works header ( 'content-type: text/plain charset=utf-8' ) print_r ( $headers_arr ) exit () Retrieving the response headers
0 Comments
Leave a Reply. |