簡體   English   中英

PHP POST 數據到 HTTPS 失敗,出現 404 錯誤! 即使使用 (CURL, fopen 和 file_get_contents)

[英]PHP POST Data to HTTPS Fail with 404 error! Even with (CURL, fopen and file_get_contents)

我所有的努力都失敗了! 即使使用CURLfopenfile_get_contents ,我也無法將數據發布到HTTPS
我總是收到404錯誤!
但是,當我使用GET方法竊取頁面時,它打開時沒有錯誤!
但是,當我對具有its same URL頁面使用POST方法時,它總是失敗並顯示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, false, $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' );
    
###########################################
?>

你能幫忙嗎? 多謝。

問題是我忘記傳遞一些餅干..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM