0

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.

27
  • What's $response when it does not work? Commented Sep 6, 2014 at 20:16
  • symptoms don't make any sense. Opening url in browser is completely disconnected from a curl request from server Commented Sep 6, 2014 at 20:17
  • 2
    "It fails" "It doesn't work" Eh Commented Sep 6, 2014 at 20:22
  • 1
    Also your API call doesn't seem to have anything in common with the documented API. You have no secret key and you did not MD5 the token. Commented Sep 6, 2014 at 20:23
  • 1
    @Petar: Um, I disagree... Commented Sep 6, 2014 at 20:25

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.