M
M
Maxim2013-12-27 14:25:21
PHP
Maxim, 2013-12-27 14:25:21

How to send a POST request using HTTP context via cURL?

It is necessary to go through authorization on httsp using certificates, since the https connection on hosting can only be used via cURL, the question arises - how to connect a certificate?
The code is how I do it via cURL, I'm not sure if the certificate is connected:

$url = "https://api.direct.yandex.ru/json-api/v4/";
$request = $request = json_encode($data);
$opts = array(
    'http'=>array(
        'method'=>"POST",
        'content'=>$request,
    )
); 
 
$context = stream_context_create($opts);
stream_context_set_option($context, 'ssl', 'local_cert', '1/solid-cert.crt');

$ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        $data = curl_exec($ch);
        curl_close($ch);

Working example using file_get_contents:
$request = $request = json_encode($data);
$opts = array(
    'http'=>array(
        'method'=>"POST",
        'content'=>$request,
    )
); 
 
$context = stream_context_create($opts);
stream_context_set_option($context, 'ssl', 'local_cert', '1/solid-cert.crt');
$result = file_get_contents('https://api.direct.yandex.ru/json-api/v4/', 0, $context);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2013-12-27
@go3l337

Solved:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSLCERT, '1/solid-cert.crt');

S
Sergey Vinogradov, 2013-12-27
@pettson

As far as I know cURL has special options for configuring SSL.
www.php.net/curl_setopt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question