简体   繁体   中英

PHP Curl doesn't return a response

I'm trying to get the response back from an API using curl in php but for some reason it fails. However, when I open the API call link in a browser it works fine. Furthermore, if I try to get the response with curl after I've opened the link in the browser curl works but if I change the $url variable to a new link it again stops working until I open it in the browser again.

Here is my code, don't worry about the api key it's just a test:

<?php 
set_time_limit(30); 
$API_KEY = "ak_wujpBbfefrmxDleyAmnqtFpqAcmey";

$url = 'https://www.google.com/'; 
$api_call = "https://www.screenshot-website.com/api/$API_KEY?url=$url&type=tablet";

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $api_call,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_TIMEOUT_MS => 30000,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_CONNECTTIMEOUT_MS => 30000 )
);

$response = json_decode(curl_exec($curl), true);

if($errno = curl_errno($curl)) {
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}"; 
}

curl_close($curl); ?>

<img src="<?php echo $response['image']; ?>">

I get no errors at all.

I think your problem is with the "https" since curl set ssl_verifier to true "The problem is that cURL has not been configured to trust the server's HTTPS certificate." you can read the full information here.

You could try setting this option:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM