Php file_get_contents()和Curl
我在laravel 4.2代码中使用file_get_contents()php函数,在登录facebook后获取个人资料照片 当我使用这个时,它工作正常Php file_get_contents()和Curl,php,security,ssl,curl,Php,Security,Ssl,Curl,我在laravel 4.2代码中使用file_get_contents()php函数,在登录facebook后获取个人资料照片 当我使用这个时,它工作正常 $arrContextOptions=array( "ssl"=>array( "verify_peer"=> false, "verify_peer_name"=> false, ), ); $content = file_get_contents($myurl, false
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=> false,
"verify_peer_name"=> false,
),
);
$content = file_get_contents($myurl, false, stream_context_create($arrContextOptions));
但它在系统中制造了一个安全漏洞,正如之前在同一问题的另一个问题中提到的,
如果我不使用这种安全漏洞方法,错误就会在我面前爆发,我无法处理
错误异常(E_警告)
帮助
文件\u get\u contents():SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3\u获取\u服务器\u证书:证书验证失败”
然后我尝试了卷曲法
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$myURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$content = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $content);
它没有犯任何错误,也没有工作,也没有“没有照片返回”!
那么,我如何才能以一种安全、干净的方式获取个人资料照片,并使用php(Laravel4.2)保存它呢?!
我正在使用XAmpp工作代码在本地主机上测试它:
$user_id = PUT_USER_ID_HERE;
$url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large';
$finalUrl = get_final_url($url);
$path = 'img/pic_facebook.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$finalUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$content = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $content);
function get_final_url( $url, $timeout = 5 )
{
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
$headers = get_headers($response['url']);
$location = "";
foreach( $headers as $value )
{
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_final_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
if ( preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
)
{
return get_final_url ( $value[1] );
}
else
{
return $response['url'];
}
}
从此处获取的函数:工作代码:
$user_id = PUT_USER_ID_HERE;
$url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large';
$finalUrl = get_final_url($url);
$path = 'img/pic_facebook.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$finalUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$content = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $content);
function get_final_url( $url, $timeout = 5 )
{
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
$headers = get_headers($response['url']);
$location = "";
foreach( $headers as $value )
{
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_final_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
if ( preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
)
{
return get_final_url ( $value[1] );
}
else
{
return $response['url'];
}
}
从此处获取的函数:可能会添加以下位置,如此处所述:curl\u setopt($ch,CURLOPT\u FOLLOWLOCATION,true);刚刚尝试过,curl仍然不返回任何内容,没有编译错误!谢谢响应的状态代码是什么?您可以添加:curl\u setopt($ch,CURLOPT\u HEADER,true);和echo$content查看响应的标题是什么,这有助于发现问题。也许可以添加以下位置,如下所示:curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);刚刚尝试过它,curl仍然不返回任何内容,没有编译错误!谢谢响应的状态代码是什么?您可以添加:curl_setopt($ch,CURLOPT_HEADER,true);和echo$content要查看响应的标题是什么,它可以帮助查找问题:SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_证书:证书验证失败感谢@Pmaniora尝试帮助我现在也尝试了此操作,但仍然是相同的旧错误。SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_证书:c证书验证失败编辑生成旧错误get_标头():SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_证书:证书验证失败感谢@Pmaniora尝试帮助我现在也尝试了此操作,但仍然是相同的旧错误。SSL操作失败,代码为1。OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_证书:c证书验证失败