php curl returning NULL, but the link works in the browser - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, October 2, 2022

php curl returning NULL, but the link works in the browser

My PHP script is supposed to fetch this link (and many similar ones). The links work in my browser, returning JSON-documents.

However, my PHP script returns NULL. How can that be?

<?php
# display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, 'my-name');

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$obj = file_get_contents_curl("https://api.crossref.org/journals/0002-8614/works?query=since-pub-date:2021-10-01&select=is-referenced-by-count&rows=1000&mailto=my@example.org&cursor=*&debug=true");
$obj = json_decode($obj, true);
   
var_dump($obj); # returns NULL
?>

No error messages are shown, but $obj returns NULL.

I would be grateful for your help!


P. S. The status code returns 0...

<?php
# this part is embedded in a function similar to file_get_contents_curl();

$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
return $status_code; 

# ... and it returns a 0 in my case!


source https://stackoverflow.com/questions/73920322/php-curl-returning-null-but-the-link-works-in-the-browser

No comments:

Post a Comment