Asked  7 Months ago    Answers:  5   Viewed   34 times

I want to make HTTPS request through PHP to a server and get the response.

something similar to this ruby code

  http = Net::HTTP.new("www.example.com", 443)

  http.use_ssl = true

  path = "uri"

  resp, data = http.get(path, nil)

Thanks

 Answers

40

this might work, give it a shot.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);

for more info, check http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Wednesday, March 31, 2021
 
kwhohasamullet
answered 7 Months ago
66

Problem appears to be specific to PhpStorm as issue can be reproduced on another machine running windows 10

Fix

When placing test.php and test2.php into my XAMPP 7.1 localhost directory, the issue does not appear, this also resolves the issue when placing files into WAMP directory on windows 10

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
30

Yes this would be a Cross domain posting and would be blocked by the browser.

Thursday, June 24, 2021
 
SubniC
answered 4 Months ago
79

Set the host header for the request:

const https = require('https');

https.get('https://AA.BB.CC.DD', {
  headers : { host : 'api.example.com' }
}, res => {
  console.log('okay');
}).on('error', e => {
  console.log('E', e.message);
});

EDIT: I dug around a bit to see how this works exactly. To allow HTTPS-based virtual hosting, there's a TLS extension called SNI (Server Name Indication). This extension is used by a client to indicate the hostname to which it is trying to connect, so the server can pick the appropriate certificate that belongs to that hostname.

Node's tls module, which is used by https, has an option servername to set this hostname:

https.get('https://AA.BB.CC.DD', {
  servername : api.example.com'
}, ...)

However, you still need to pass a Host header too (that's part of the regular HTTP protocol):

https.get('https://AA.BB.CC.DD', {
  headers : { host : 'api.example.com' },
  servername : 'api.example.com'
}, ...)

To keep things DRY, Node.js will set servername to the Host header, unless it's already set to something else (here).

Wednesday, August 11, 2021
 
SheppardDigital
answered 3 Months ago
72

I managed to get it all working asyncronously with both cookies and unsigned https.

I used the code here:

http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/

and modified for unsigned https using Brian Yarger's code here:

Self-signed SSL acceptance on Android

(Add the above code to the beginning of run() in HttpConnection.java)

To get the cookies to work, I had to modify some code (POST snippet from HttpConnection.java):

         case POST:
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(data));
            httpPost.addHeader("Cookie", Cookie.getCookie());
            response = httpClient.execute(httpPost);

            Header[] headers=response.getAllHeaders();
            for(int i=0;i<headers.length;i++){
                if(headers[i].getName().equalsIgnoreCase("Set-Cookie")){
                    //Log.i("i",headers[i].getName()+"---"+headers[i].getValue());
                    Cookie.setCookie(headers[i].getValue());
                    break;
                }
            }
            break;

Many thanks to everyone for pointing me in the direction,

Wednesday, September 29, 2021
 
ttran4040
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share