问题 通过PHP发出HTTPS请求并获得响应


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

类似于这个ruby代码的东西

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

  http.use_ssl = true

  path = "uri"

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

谢谢


6492
2017-10-06 13:54


起源



答案:


这可能会奏效,试一试。

 $ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
//设置为curl_exec返回结果而不是输出结果。
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
//获取响应并关闭频道。
$ response = curl_exec($ ch);
curl_close($ CH);

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


11
2017-10-06 14:04



感谢链接解释了很多 - wael34218
链接很完美!你可以更新你的答案至少有curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);线 - rsc


答案:


这可能会奏效,试一试。

 $ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
//设置为curl_exec返回结果而不是输出结果。
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
//获取响应并关闭频道。
$ response = curl_exec($ ch);
curl_close($ CH);

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


11
2017-10-06 14:04



感谢链接解释了很多 - wael34218
链接很完美!你可以更新你的答案至少有curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);线 - rsc


Zend框架 有一个很好的组件叫 一个Zend_Http_Client 这种交易非常适合。

它使用的引擎盖下 卷曲 发出请求,但你会发现Zend_Http_Client有一个更好的界面可以使用,并且当你想要添加自定义标题或使用响应时更容易配置。

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

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

2
2017-10-06 14:05



404 ........................... - Pacerier
更新了链接 - David Snabel-Caunt


示例如何使用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 = 'user@example.com: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>";
?>

0
2017-12-13 01:05





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

GET示例:

<?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;
}
?>

发布示例

<?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;
}
?>

0
2017-11-05 10:34



OP要求 HTTPS - jimasun