How to make a POST request via HTTPS in PHP?

I am trying to work with the YouTube API and ClientLogin . And that means I need to make a POST request to my servers.

The URL where I need to make the request is https://www.google.com/accounts/ClientLogin . The variables that I need to send it Email, Passwd, sourceand service. So far so good.

I found this neat feature for making POST calls (see below), but it doesn't use HTTPS, which I think should use. This all works, but I think my POST request is being forwarded to HTTPS and therefore it does not give me the proper callback. When I try var_dump, the returned data web page reloads, and I end up at https://www.google.com/accounts/ClientLogin where I get the correct data. But, of course, I need this data as an array or string.

So, how do I make a POST request using HTTPS?

Se my code (which I found in the Jonas Snippet Library ) below:

function post_request($url, $data, $referer='') {

        $data = http_build_query($data);

        $url = parse_url($url);     

        $host = $url['host'];
        $path = $url['path'];

        $fp = fsockopen($host, 80, $errno, $errstr, 30);

        if ($fp){

            fputs($fp, "POST $path HTTP/1.1\r\n");
            fputs($fp, "Host: $host\r\n");

            if ($referer != '')
                fputs($fp, "Referer: $referer\r\n");

            fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            fputs($fp, "Content-length: ". strlen($data) ."\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $data);

            $result = ''; 
            while(!feof($fp)) {

                $result .= fgets($fp, 128);
            }
        }
        else { 
            return array(
                'status' => 'err', 
                'error' => "$errstr ($errno)"
            );
        }

        fclose($fp);

        $result = explode("\r\n\r\n", $result, 2);

        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : '';

        return array(
            'status' => 'ok',
            'header' => $header,
            'content' => $content
        );
    }

These are the response headers:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Tue, 03 May 2011 12:15:20 GMT
Expires: Tue, 03 May 2011 12:15:20 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 728
Server: GSE
Connection: close

The content I receive is some form of autosubmitted, which I think is related to the fact that I use HTTP instead of HTTPS:

    function autoSubmit() {
      document.forms["hiddenpost"].submit();
    }

Processing...

, POST- HTTPS?


octopusgrabbus, 443 80. , .

var_dump return:

array(3) {
  ["status"]=>
  string(2) "ok"
  ["header"]=>
  string(0) ""
  ["content"]=>
  string(0) ""
}

. ?

+3
3

, HTTPS, HTTP , . , ssl php. , , , .

cURL (URL- ), GET POST, https.

+6

80. SSL - 443.

+2

SSL, , -. , .

0

All Articles