如何使用cURL和PHP从使用SSL的网站获取数据



我正试图从一个需要SSL证书才能连接到的网站获取一些数据。我发现了以下代码:

// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $host);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $host);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);

cURL查询运行良好,但我得到的数据是这样的:

400没有所需的SSL证书已发送

400错误请求

没有所需的SSL证书send
nginx

我要找的是网站的index.php(和其他路径(中包含的数据。那么,我该如何向代码中添加证书,并使用cURL从网站获取数据呢?PS:数据会是JSON格式吗?PPS:如果这可以帮助,我正在使用PHPStorm

试试这个。

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://jsonplaceholder.typicode.com/todos/1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array("content-type: application/json"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

你能这样试试吗?echo$str=file_get_contents(";https://jsonplaceholder.typicode.com/todos/1"(;

最新更新