如何解决PHP POST 数据到 HTTPS 失败,出现 404 错误!即使使用 (CURL, fopen 和 file_get_contents)
我所有的努力都失败了!即使使用 HTTPS
、CURL
和 fopen
,我也无法将数据发布到 file_get_contents
!
我总是收到 404
错误!
但是,当我使用 GET
方法窃取页面时,它打开时没有错误!
但是当我对带有 POST
的页面使用 its same URL
方法时,它总是失败并出现 404
错误!
我的 PHP 代码:
<?php
###########################################
function curl_fopen_getContents( $functionName='curl',$method='GET' )
{
$post_data = http_build_query(array( 'a' => '1' ));
$cookies = 'a=1';
$opts = array('http' => array(
'method' => strtoupper($method),'header' =>
"Content-type: application/x-www-form-urlencoded\r\n"
."Content-Length: " . strlen($post_data)."\r\n"
."Cookie: $cookies\r\n",'content' => $post_data,'timeout' => 60
));
$context = stream_context_create($opts);
///////////////////////////////////
// you can decode it
$https_url = 'my_url_here';
if( $functionName=='fopen' )
{
$result = fopen($https_url,'r',false,$context); @fpassthru($result); @fclose($result);
}
elseif( $functionName=='file_get_contents' )
{
$result = file_get_contents($https_url,$context);
}else{
$result = curl_post($https_url,$method,$post_data,$cookies);
}
return $result;
}
###########################################
function curl_post( $https_url,$method='GET',$post_data='',$cookies='' )
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$https_url);
curl_setopt($ch,CURLOPT_COOKIE,$cookies);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
#curl_setopt($ch,CURLOPT_HEADER,1);
if( strtoupper($method) !='GET' )
{
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
}
curl_setopt($ch,CURLOPT_FAILONERROR,1);
$data = curl_exec($ch); if(!$data){ $data=curl_error($ch); }
curl_close($ch);
return $data;
}
###########################################
/*
curl_fopen_getContents( $functionName,$method );
$functionName = curl/fopen/file_get_contents
$method = GET/POST
*/
echo curl_fopen_getContents( 'curl','GET' );
###########################################
?>
你能帮忙吗?非常感谢。
解决方法
问题是我忘记传递一些 cookie..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。