Laravel和共享主机CURL无法正常工作

时间:2017-11-14 15:20:48

标签: php laravel curl hosting

我正在将我的Laravel应用程序部署到Godaddy的共享主机,仅用于测试目的(以便我可以在域中共享)以进行生产我将使用更好的解决方案。

在我的localhost中,一切正常,但在托管CURL不起作用。实际上,这个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;
} 

这不起作用。当我检查托管中的cookies.txt文件时,它总是为空,实际上我尝试将其移动到公共,另一个文件夹和所有地方,但它没有被编辑,它总是空的。它在我的localhost中运行良好,但由于某种原因它不想在托管中工作。

有没有人有想法解决这个问题?

这是我的curinfo,如果它有帮助

"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
]

1 个答案:

答案 0 :(得分:1)

最有可能的是,你被Go​​Daddy阻止了防火墙。我在共享的虚拟主机上看到过无数次。你可能应该投资一个VPS(他们在ramnode上只需3.5美元/月),但你也犯了一些错误,即你忽略了curl_setopt给出的任何错误,它返回bool(false)失败,你的代码完全忽略了。试着改用:

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;
}

然而,在调试curl时,你应该总是启用CURLOPT_VERBOSE并获取详细日志,它在调试curl代码时通常非常有用,比如

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);
    }
}