1

I am deploying my Laravel application to shared hosting from Godaddy for testing purposes only (so that I can share it in a domain) for production I will be using a better solution.

In my localhost, everything works fine, but in hosting CURL is not working. Actually, this CURL

function setUrl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/cookies/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, public_path().'/cookies/cookies.txt');
    $buffer = curl_exec($ch);
    curl_close($ch);
    return $buffer;
} 

This does not work. When I check my cookies.txt file in the hosting it is always empty, actually I tried moving it to public, another folder and everywhere, but it does not get edited, it is always empty. It worked fine in my localhost, but for some reason it doesn't want to work in hosting.

Does anyone have some idea to tackle this issue?

This I my curinfo if it helps

"content_type" => null
  "http_code" => 0
  "header_size" => 0
  "request_size" => 0
  "filetime" => -1
  "ssl_verify_result" => 0
  "redirect_count" => 0
  "total_time" => 0.200232
  "namelookup_time" => 4.8E-5
  "connect_time" => 0.0
  "pretransfer_time" => 0.0
  "size_upload" => 0.0
  "size_download" => 0.0
  "speed_download" => 0.0
  "speed_upload" => 0.0
  "download_content_length" => -1.0
  "upload_content_length" => -1.0
  "starttransfer_time" => 0.0
  "redirect_time" => 0.0
  "redirect_url" => ""
  "primary_ip" => ""
  "certinfo" => []
  "primary_port" => 0
  "local_ip" => ""
  "local_port" => 0
]
6
  • Is curl enabled on the server? Commented Nov 14, 2017 at 15:28
  • @buzzfizz it's enabled by default, I have checked it and it does recognize curl commands Commented Nov 14, 2017 at 15:29
  • echo phpinfo(); in a separate file and check whether curl is enabled on a server or not. If not then contact the support team. Commented Nov 14, 2017 at 15:31
  • Any reason why are you using curl instead of php's own file writing? Commented Nov 14, 2017 at 15:32
  • Either the text file has insufficient permission or the public_path() returns a different path. Commented Nov 14, 2017 at 15:34

1 Answer 1

1

most likely, you're being firewall-blocked by GoDaddy. i've seen same shit countless times on shared webhosts. you should probably invest in a VPS instead (they're like $3.5/month on ramnode..), but you're also doing some mistakes, namely you ignore any errors given by curl_setopt, it returns bool(false) on failure, which your code completely ignores. try using this instead:

function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' .  curl_errno ($ch) .' curl_error: '.curl_error($ch) );
    }
    return true;
}

however, when debugging curl, you should always enable CURLOPT_VERBOSE and get the verbose log, it's usually super useful when debugging curl code, like

function setUrl($url) {
    $debugfileh=tmpfile();
    $ch = curl_init();
    try{
    ecurl_setopt($ch, CURLOPT_URL, $url);
    ecurl_setopt($ch, CURLOPT_VERBOSE, 1);
    ecurl_setopt($ch, CURLOPT_STDERR, $debugfileh);
    ecurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    ecurl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    ecurl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/cookies/cookies.txt');
    ecurl_setopt($ch, CURLOPT_COOKIEFILE, public_path().'/cookies/cookies.txt');
    $buffer = curl_exec($ch);
    return $buffer;
    }finally{
    var_dump('curl verbose log:',file_get_contents(stream_get_meta_data($debugfileh)['uri']));
    fclose($debugfileh);
    curl_close($ch);
    }
} 
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.