Created
March 2, 2023 03:18
-
-
Save ngohuytrieu/42670af2d6341a9c896a8406dbd9c12d to your computer and use it in GitHub Desktop.
PHP curl get headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = "https://www.google.com/"; | |
$curlObj = curl_init(); | |
curl_setopt($curlObj, CURLOPT_URL, $url); | |
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlObj, CURLOPT_HEADER, 1); // must set this line | |
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($curlObj); | |
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', | |
$result, $match_found); | |
$cookies = array(); | |
foreach($match_found[1] as $item) { | |
parse_str($item, $cookie); | |
$cookies = array_merge($cookies, $cookie); | |
} | |
print_r( $cookies); | |
curl_close($curlObj); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment