How to set curl options

Created on 31 October 2023, about 1 year ago
Updated 2 July 2024, 5 months ago

Is there a way to set curl options, either in one of the .yml files or in the requests themselves? Our firewall swaps a custom certificate in, which angers curl and prevents me from testing services from my local environment, throwing curl error 60 on each request. I've tried pointing curl.cainfo, openssl.cafile, and openssl.capath each at a copy of our custom CA file, without any change in behavior. Also tried setting it in a .curlrc file.

I think if I can just temporarily disable verifypeer while I'm troubleshooting locally, that would get me around the problem...I found another issue with a similar question, but unfortunately the poster closed it without revealing the solution they found. Thanks to anyone who can help!

💬 Support request
Status

Closed: works as designed

Version

9.3

Component

Miscellaneous

Created by

🇺🇸United States wrd-oaitsd

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @wrd-oaitsd
  • 🇮🇹Italy emanuelrighetto Verona

    Hi @ wrd-oaitsd
    sorry for the huge delay.

    You can achieve what you want by adding options to your "Http Services Api" definition via *.http_services_api.yml file.
    sorry for the huge delay.
    You can achieve what you want by adding options to your "Http Services Api" definition via "*.http_services_ api.yaml" file.
    Take a look at the example implementation here https://git.drupalcode.org/project/http_client_manager/-/blob/10.x/modules/http_client_manager_example/http_client_manager_example.http_services_api.yml?

    You can add any custom value like these:

    example_services:
      title: "[JSON] - Example Services fake API"
      api_path: "src/api/example_services.json"
      config:
        base_uri: "http://jsonplaceholder.typicode.com"
        curl:
          'CURLOPT_SSL_VERIFYPEER': true
          'CURLOPT_CAINFO': "/path/to/cacert.pem"
    

    You can also achieve the same result by entering extra configurations in the configuration form at 'admin/config/services/http-client-manager/settings':

    But if you have more complex needs you can exploit the "onHandlerStack" event, see the example here: https://git.drupalcode.org/project/http_client_manager/-/blob/10.x/modules/http_client_manager_example/src/EventSubscriber/HttpClientManagerExampleSubscriber.php

    You can create your own EventSuscriber where you can override the handler as you wish:

      /**
       * This method is called whenever the http_client.handler_stack event is
       * dispatched.
       *
       * @param \Drupal\http_client_manager\Event\HttpClientHandlerStackEvent $event
       *   The HTTP Client Handler stack event.
       */
      public function onHandlerStack(HttpClientHandlerStackEvent $event) {
        if ($event->getHttpServiceApi() != 'example_services') {
          return;
        }
    
        $stack = $event->getHandlerStack();
        $stack->setHandler(new CustomCurlHandler());
      }
    

    where CustomCurlHandler is defined like that:

    
    declare(strict_types=1);
    
    namespace Drupal\your_custom_module;
    
    use GuzzleHttp\Handler\CurlHandler;
    use GuzzleHttp\Promise\PromiseInterface;
    use Psr\Http\Message\RequestInterface;
    
    class CustomCurlHandler extends CurlHandler {
    
      /**
       * @param RequestInterface $request
       * @param array $options
       * @return PromiseInterface
       */
      public function __invoke(RequestInterface $request, array $options): PromiseInterface
      {
        // Change cURL options here
        $options['curl'][CURLOPT_SSL_VERIFYPEER] = false;
        $options['curl'][CURLOPT_TIMEOUT] = 666;
    
        return parent::__invoke($request, $options);
      }
    }
    
  • Status changed to Closed: works as designed 5 months ago
  • 🇮🇹Italy aronne

    Thanks @emanuelrighetto for your support.

    Regards,
    aronne

Production build 0.71.5 2024