Home  >  Q&A  >  body text

php - curl_exec returns false, curl_error returns empty

The return value of curl_exec($ch) is always false, and calling curl_errno($ch) is empty? ? ?

public function http_curl($url,$type='get',$res='json',$arr=''){
    // 初始化
    $ch=curl_init();
    // 设置参数
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_HEADER,FALSE);
    if($type == 'post'){
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
    }
    $output=curl_exec($ch);
    curl_close($ch);
    var_dump($output);
    echo 'error:'.curl_errno($ch);
    if(curl_errno($ch)){
        var_dump(curl_errno($ch));
    }
    if($res == 'json'){
        return json_decode($output,true); 
    }
        return $output;
}

滿天的星座滿天的星座2728 days ago1929

reply all(3)I'll reply

  • 龍

    2019-05-16 15:23:58

    What should I do if my curl_init returns empty?

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-24 11:35:47

    Thanks for the invitation!

    curl_errno($ch) You have closed the handle before, change the order and then print.

    reply
    0
  • 迷茫

    迷茫2017-05-24 11:35:47

    It would be strange to still get errors after closing the connection. . Get the error before curl_close

    reply
    0
  • Cancelreply