开发者

How do I make a POST request over HTTPS in PHP?

开发者 https://www.devze.com 2023-03-02 17:39 出处:网络
I trying to work with the YouTube API and its ClientLogin. And that means that I need to make a POST request to their servers.

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

The URL to which I need to make the request to https://www.google.com/accounts/ClientLogin. The variables I need to send are Email, Passwd, source and service. So far, so good.

I found this neat function to make POST calls (see below), but it does not use HTTPS, which I think I must use. It 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 to var_dump, the returned data web page reloads and I end up at https://www.google.com/accounts/ClientLogin where I get proper 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 at 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");
            fp开发者_JAVA百科uts($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
        );
    }

This is 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 get back is some kind of form autosubmitted, which I think is because I use HTTP instead of HTTPS:

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

Processing...

So, how do I do a HTTPS POST request?


As octopusgrabbus kindly pointed out, I need to use port 443 instead of 80. So I changed this, but now I get nothing back.

var_dump from function return:

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

I get no header and no content back. What is wrong?


I think you cannot talk directly HTTPS, as it is HTTP encrypted with the public certificate of the server you are connecting to. Maybe you can use some of the ssl functions in php. But, this will take you some time and frankly, there are easier things.

Just take a look at cURL (client URL), that has support for GET and POST requests, and also connecting to https servers.


You are opening your socket at port 80. The SSL port is 443.


If this is SSL, there is an official computer name tied to the secure cert that's present on that web server. You might need to connect using that official name.


When you open the socket, changing the port to 443 and the prepending the host with ssl:// should work. (I just had this issue with paypal and some third party code). This assumes you don't have a protocol in your host already.

So

 $fp = fsockopen('ssl://' . $host, 443, $errno, $errstr, 30);

As Carlos pointed out cUrl is good for this sort of thing. But there's no need to completely change what you're using in this case, particularly when it's a single line change.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号