PHP CURL redirect

curl, php, redirect

Solution

Set `CURLOPT_FOLLOWLOCATION` to `true` to make cURL follow a redirect.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Source: http://php.net/manual/en/function.curl-setopt.php

Problem

im pretty new in php. Current I'm facing problem about php curl post and redirection. I had wrote plenty of code to connect a cross-domain api. In this API, the response will be redirect my page into another page. so, now what I'm facing was I can't follow the api response as well. It means in my php scripts, my curl had receive the redirection page web content but my page still remaining. May I know is php curl can be follow the API redirection? Is there anyone know how to solve this issue? Below will be my code. ``` $url = "http://www.google.com.my"; //example this is an API $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_REFERER, $url); curl_setopt ($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string))); $result = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); ``` In the $result, i had receive the API redirect page. But it is only web content.

Original source