PHP cURL: Get target of redirect, without following it

前端 未结 5 1294
梦毁少年i
梦毁少年i 2021-01-02 10:47

The curl_getinfo function returns a lot of metadata about the result of an HTTP request. However, for some reason it doesn\'t include the bit of information I want at the m

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 11:10

    You can simply use it: (CURLINFO_REDIRECT_URL)

    $info = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo $info; // the redirect URL without following it
    

    as you mentioned, disable the CURLOPT_FOLLOWLOCATION option (before executing) and place my code after executing.

    CURLINFO_REDIRECT_URL - With the CURLOPT_FOLLOWLOCATION option disabled: redirect URL found in the last transaction, that should be requested manually next. With the CURLOPT_FOLLOWLOCATION option enabled: this is empty. The redirect URL in this case is available in CURLINFO_EFFECTIVE_URL

    Refrence

提交回复
热议问题