How to get data from a website that uses SSL using cURL and PHP?

微笑、不失礼 提交于 2021-02-05 11:34:26

问题


I am trying to get some data from a website where you need to have a SSL certificate to be able to connect to it. I have found the following code :

// 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);

The cURL query works well, but the data that I get is this one :

400 No required SSL certificate was sent

400 Bad Request

No required SSL certificate was sent
nginx

What I am looking for is the data contained in the index.php (and the other paths) of the website. So, how can I do to add a Certificate to the code, and, using cURL, get the data from the website ? PS : Will the data will be in JSON format ? PPS : if this can be helpfull, I am using PHPStorm


回答1:


Try this.

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



回答2:


can you try like this? echo $str = file_get_contents("https://jsonplaceholder.typicode.com/todos/1");



来源:https://stackoverflow.com/questions/65437314/how-to-get-data-from-a-website-that-uses-ssl-using-curl-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!