PHP:如何使用system()/ exec()的curl始终工作?

时间:2018-01-05 12:22:07

标签: php curl

我们的Web服务器上的PHP版本很旧,并且在curl调用中不支持TLSv1.2。所以我试图用system()/exec()进行卷曲调用。

  

这有时有效但不是。

失败时没有错误。然后参考这个建议 - https://stackoverflow.com/a/539030/1278063我在命令的末尾添加了2>&1,将错误从stderr重定向到stdout。 以下是示例代码 -

<?php
    $cmd = sprintf('curl -X POST %s --tlsv1.2 2>&1', escapeshellarg($url));
    $sysout = exec($cmd,$output,$ret);
    print_r($output);
?>

代码失败时的输出如下 -

Array ( 
    [0] => mkdir: cannot create directory `/usr/bin/.libs': Permission denied 
    [1] => /usr/bin/curl: line 202: cd: /root/curl-7.57.0/src: Not a directory 
    [2] => gcc: curl-slist_wc.o: No such file or directory 
    [3] => gcc: curl-tool_binmode.o: No such file or directory 
    [4] => gcc: curl-tool_bname.o: No such file or directory 
    [5] => gcc: curl-tool_cb_dbg.o: No such file or directory 
    [6] => gcc: curl-tool_cb_hdr.o: No such file or directory 
    [7] => gcc: curl-tool_cb_prg.o: No such file or directory 
    [8] => gcc: curl-tool_cb_rea.o: No such file or directory 
    [9] => gcc: curl-tool_cb_see.o: No such file or directory 
    [10] => gcc: curl-tool_cb_wrt.o: No such file or directory 
    [11] => gcc: curl-tool_cfgable.o: No such file or directory 
    [12] => gcc: curl-tool_convert.o: No such file or directory 
    [13] => gcc: curl-tool_dirhie.o: No such file or directory 
    [14] => gcc: curl-tool_doswin.o: No such file or directory 
    [15] => gcc: curl-tool_easysrc.o: No such file or directory 
    [16] => gcc: curl-tool_formparse.o: No such file or directory 
    [17] => gcc: curl-tool_getparam.o: No such file or directory 
    [18] => gcc: curl-tool_getpass.o: No such file or directory 
    [19] => gcc: curl-tool_help.o: No such file or directory 
    [20] => gcc: curl-tool_helpers.o: No such file or directory 
    [21] => gcc: curl-tool_homedir.o: No such file or directory 
    [22] => gcc: curl-tool_hugehelp.o: No such file or directory 
    [23] => gcc: curl-tool_libinfo.o: No such file or directory 
    [24] => gcc: curl-tool_main.o: No such file or directory 
    [25] => gcc: curl-tool_metalink.o: No such file or directory 
    [26] => gcc: curl-tool_msgs.o: No such file or directory 
    [27] => gcc: curl-tool_operate.o: No such file or directory 
    [28] => gcc: curl-tool_operhlp.o: No such file or directory 
    [29] => gcc: curl-tool_panykey.o: No such file or directory 
    [30] => gcc: curl-tool_paramhlp.o: No such file or directory 
    [31] => gcc: curl-tool_parsecfg.o: No such file or directory 
    [32] => gcc: curl-tool_strdup.o: No such file or directory 
    [33] => gcc: curl-tool_setopt.o: No such file or directory 
    [34] => gcc: curl-tool_sleep.o: No such file or directory 
    [35] => gcc: curl-tool_urlglob.o: No such file or directory 
    [36] => gcc: curl-tool_util.o: No such file or directory 
    [37] => gcc: curl-tool_vms.o: No such file or directory 
    [38] => gcc: curl-tool_writeout.o: No such file or directory 
    [39] => gcc: curl-tool_xattr.o: No such file or directory 
    [40] => gcc: ../lib/curl-strtoofft.o: No such file or directory 
    [41] => gcc: ../lib/curl-nonblock.o: No such file or directory 
    [42] => gcc: ../lib/curl-warnless.o: No such file or directory 
    [43] => gcc: ../lib/.libs/libcurl.so: No such file or directory 
)

为什么代码有时会起作用?如何使它始终工作?

1 个答案:

答案 0 :(得分:0)

在最新的系统上编译静态curl库(应该是./configure --disable-shared --with-ssl)并将静态curl库复制到旧系统,现在TLS应该可以正常工作。但是因为从个人经验来看,静态编译有时会因各种原因而失败,沉默(至少使用gcc),你应该验证它是静态的,例如ldd ./curl(如果它是静态的,它会抱怨curl是静态的,或打印它试图加载的lib列表。)