2

i have a problem with curl, i trying to get content using CURL but return just null, i dont know what i miss, please tell me, here is my code :

$url = "https://shopee.co.id/api/v1/search_items/?by=pop&order=desc&keyword=yi 067&newest=0&limit=50&page_type=shop&match_id=16775174";
$html = file_get_contents_curl($url);
var_dump($html);

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_ENCODING, 0);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'));


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

    return $data;
}
CSM
  • 35
  • 1
  • 7
  • 1
    You're missing some error reporting. I'm guessing something with SSL. – IncredibleHat Mar 30 '18 at 15:06
  • You should see this answer : [a link](https://stackoverflow.com/questions/4372710/php-curl-https) – Yassine Mar 30 '18 at 15:07
  • I used this exact code and got some HTML back, but that html defined a trivial document with body content. @CSM are you sure it's returning NULL and not FALSE or the empty string? – S. Imp Mar 30 '18 at 15:18

1 Answers1

2

With curl, it's often a good idea to use curl_getinfo to obtain additional information about your curl connection before closing it. In your case, the NULL/FALSE/empty result could be due to a number of reasons and inspecting the curl info might help you find more detail. Here's a modified version of your function that uses this function to obtain additional information. You might consider writing print_r($info, TRUE) to a log file or something. It might be empty because the server response is empty. It might be false because the url cannot be reached from your server due to firewall issues. It might be returning an http_code that is 404 NOT FOUND or 5XX.

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_ENCODING, 0);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    $data = curl_exec($ch);

    $info = curl_getinfo($ch);


    if(curl_errno($ch)) {
        throw new Exception('Curl error: ' . curl_error($ch));
    }

    curl_close($ch);

    if ($data === FALSE) {
        throw new Exception("curl_exec returned FALSE. Info follows:\n" . print_r($info, TRUE));
    }

    return $data;
}

EDIT: I added curl_errno checking too.

S. Imp
  • 2,833
  • 11
  • 24
  • i dont know but i try still null – CSM Mar 30 '18 at 15:48
  • @CSM I edited my post and added a curl_errno error check too. Try that function. I'd be surprised if you received a NULL value to be honest. It's probably the empty string? – S. Imp Mar 30 '18 at 16:18
  • thank you very much master @S. lmp yesterday this is work fine :) – CSM Apr 01 '18 at 13:33