我对 php 很陌生。目前我面临有关 php curl post 和重定向的问题。我已经编写了大量代码来连接跨域 api。在此 API 中,响应会将我的页面重定向到另一个页面。
所以,现在我面临的是我也无法遵循 api 响应。
这意味着在我的 php 脚本中,我的curl 已收到重定向页面的网页内容,但我的页面仍然保留。
我可以知道 php curl 可以遵循 API 重定向吗?
有谁知道如何解决这个问题?
下面是我的代码。
$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);
在 $result 中,我收到了 API 重定向页面。但这只是网络内容。