通过 PHP 发出 HTTPS 请求并得到响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3873187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:21:25  来源:igfitidea点击:

Make a HTTPS request through PHP and get response

phphttpsrequest

提问by wael34218

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

我想通过 PHP 向服务器发出 HTTPS 请求并获得响应。

something similar to this ruby code

类似于这个 ruby​​ 代码的东西

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

  http.use_ssl = true

  path = "uri"

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

Thanks

谢谢

回答by saadlulu

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/

有关更多信息,请查看 http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

回答by David Snabel-Caunt

The Zend Frameworkhas a nice component called Zend_Http_Clientwhich is perfect for this kind of transaction.

Zend框架有一个称为一个很好的组件一个Zend_Http_Client这是完美的这种交易。

Under the hood it uses curlto make requests, but you'll find Zend_Http_Client has a much nicer interface to work with and is easier to configure when you want to add custom headers or work with responses.

在幕后,它使用curl来发出请求,但您会发现 Zend_Http_Client 有一个更好的界面可以使用,并且在您想要添加自定义标头或处理响应时更容易配置。

If all you want to do is retrieve the page contents with minimal work, you may be able to do the following, depending on your server's configuration:

如果您只想以最少的工作检索页面内容,您可以执行以下操作,具体取决于您的服务器配置:

$data = file_get_contents('https://www.example.com/');

回答by Yasir Hantoush

Example how to use HttpRequest to post data and receive the response:

示例如何使用 HttpRequest 发布数据并接收响应:

<?php
//set up variables
$theData = '<?xml version="1.0"?>
<note>
    <to>my brother</to>
    <from>me</from>
    <heading>hello</heading>
    <body>this is my body</body>
</note>';
$url = 'http://www.example.com';
$credentials = '[email protected]:password';
$header_array = array('Expect' => '',
                'From' => 'User A');
$ssl_array = array('version' => SSL_VERSION_SSLv3);
$options = array(headers => $header_array,
                httpauth => $credentials,
                httpauthtype => HTTP_AUTH_BASIC,
            protocol => HTTP_VERSION_1_1,
            ssl => $ssl_array);

//create the httprequest object               
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
//add the content type
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
//add the raw post data
$httpRequest_OBJ->setRawPostData ($theData);
//send the http request
$result = $httpRequest_OBJ->send();
//print out the result
echo "<pre>"; print_r($result); echo "</pre>";
?>

回答by Salam El-Banna

There are 2 example GET Method And POST Method

有 2 个示例 GET 方法和 POST 方法

GET example:

获取示例:

<?php
$r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
$r->setOptions(array('lastmodified' => filemtime('local.rss')));
$r->addQueryData(array('category' => 3));
try {
    $r->send();
    if ($r->getResponseCode() == 200) {
        file_put_contents('local.rss', $r->getResponseBody());
    }
} catch (HttpException $ex) {
    echo $ex;
}
?>

Post Example

发布示例

<?php
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
    echo $r->send()->getBody();
} catch (HttpException $ex) {
    echo $ex;
}
?>