C/C++ libcurl CURLOPT_NOSIGNAL选项

多线程

此接口并没有使用到SSL/TLS,但会不会是用到了signals导致的crash呢?官方建议在多线程场景下应该设置CURLOPT_NOSIGNAL选项,因为在解析DNS出现超时的时候将会发生“糟糕”的情况。官方也给出了解决方法,可以使用c-ares[2]的libcurl版本实现异步域名解析来预防这种“糟糕”的情况,但是最后一句还是告诫我们:在多线程场景下,若不设置CURLOPT_NOSIGNAL选项,可能会有“意外”的情况发生。通过官方这段描述,可以大致猜测到是没有设置这个选项造成的crash。

curl毫秒超时使用

升级后的libcurl已经支持了毫米级别的超时。但是使用的时候还有一个坑需要注意跨过去 。

libcurl如果检查到设置的timeout < 1s 那么会直接发出一个信号说 “已经超时了”

如果要使用毫秒超时 那么还需要关闭这个signal功能,即设置中需要多一个

curl_setopt($ch, CURLOPT_NOSIGNAL, 1);

但是,这样域名解析就不会受超时控制。也就是说,有可能被域名解析卡住很长时间,使用时需要注意。

然后
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);

这样就可以控制 超时为 200ms了

其他的毫秒超时同理。

//以秒为单位:
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
//以毫秒为单位:在CURL7.16.2中被加入,从PHP5.2.3起可使用
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
发布了374 篇原创文章 · 获赞 14 · 访问量 10万+
展开阅读全文

CURL_OPTPOST设置为true时,CURL http身份验证失败

04-26

<div class="post-text" itemprop="text"> <p>I'm trying to build a payment form that integrates with Firstdata's api. I need to post an XML string to their server. They also require a client side certificate and http authentication. My CURL set up currently looks like this:</p> <pre><code>function firstdata_send($config_param, $data) { $config_default = array( 'test' => FALSE, ); // settings in $config_param will overwrite settings in $config_default $config = (object)array_merge($config_default, $config_param); if($config->test) { $url = 'https://ws.merchanttest.firstdataglobalgateway.com/fdggwsapi/services/order.wsdl'; } else { $url = 'https://ws.firstdataglobalgateway.com/fdggwsapi/services/order.wsdl'; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "{$config->username}:{$config->password}"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSLCERT, $config->pemfile); curl_setopt($ch, CURLOPT_SSLKEY, $config->keyfile); curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $config->keypass); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_VERBOSE, TRUE); $result = curl_exec($ch); $result .= curl_error($ch); return $result; } </code></pre> <p>Their server responds with <code>HTTP/1.1 401 Unauthorized</code>. But if I comment out the post options:</p> <pre><code> //curl_setopt($ch, CURLOPT_POST, TRUE); //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); </code></pre> <p>I get <code>HTTP/1.1 200 OK</code>. Unless I'm completely misunderstanding whats going on it seems like using post somehow interferes with the auth headers. I don't know what I'm missing.</p> <p>Solved:</p> <p>Turns out the ssl certificates the test account had generated were bad. I had to call their tech support and they had to regenerate the certs 3 times before the system would accept them. Sorry for wasting your time. I should have called them first. If anybody is interested the tech support number I called was (888) 477-3611. I think NomikOS was closest to being correct so I'll mark his as the answer and up vote the rest of you. Thanks again.</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览