php CURLOPT_FOLLOWLOCATION 无法激活
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6352927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CURLOPT_FOLLOWLOCATION cannot be activated
提问by Dan Stern
im having some problems with curls and i dont know how to solve them.
我在卷发方面遇到了一些问题,我不知道如何解决。
the idea is to get a user's username and pw and post it into an external webpage.
这个想法是获取用户的用户名和密码并将其发布到外部网页中。
Here is the code:
这是代码:
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://sso.uc.cl/cas/login?service=https://portaluc.puc.cl/uPortal/Login"); // URL to post
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "username=$usuario&password=$pw");
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec( $ch ); // runs the post
curl_close($ch);
echo "Reply Response: " . $result; // echo reply response
here is the error:
这是错误:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/th000862/public_html/encuesta/login2.php on line 10
After that error, the user is not logged in into the external webpage.
在该错误之后,用户未登录到外部网页。
回答by Francois Deschenes
That error means that your PHP configuration is prohibiting you from following the location. There are a few ways you could work around the problem without installing additional libraries as suggested by @mario.
该错误意味着您的 PHP 配置禁止您关注该位置。有几种方法可以解决该问题,而无需按照@mario 的建议安装其他库。
- If you own the server or have root access, you could change the php.ini file to disable "safe_mode".
- You could also create a .htaccess file in your document root with
php_value safe_mode off
in it. - You may be able to add
ini_set('safe_mode', false);
in your PHP file.
- 如果您拥有服务器或具有 root 访问权限,您可以更改 php.ini 文件以禁用“safe_mode”。
- 您还可以在文档根目录中创建一个 .htaccess 文件
php_value safe_mode off
。 - 您可以
ini_set('safe_mode', false);
在 PHP 文件中添加。
If none of the above works, you could also do something along these lines:
如果以上都不起作用,您还可以按照以下方式做一些事情:
$ch = curl_init('https://sso.uc.cl/cas/login?service=https://portaluc.puc.cl/uPortal/Login');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . urlencode($usuario) . '&password=' . urlencode($pw));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$result = curl_exec($ch);
curl_close($ch);
// Look to see if there's a location header.
if ( ! empty($result) )
if ( preg_match('/Location: (.+)/i', $result, $matches) )
{
// $matches[1] will contain the URL.
// Perform another cURL request here to retrieve the content.
}
回答by The Mask
and too, you need do this: for access of pages using https protocols,you need change CURLOPT_SSL_VERIFYPEER to false.
同样,您需要这样做:为了使用 https 协议访问页面,您需要将 CURLOPT_SSL_VERIFYPEER 更改为 false。
try this:
尝试这个:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);