Home > OS >  I need this function in php
I need this function in php

Time:04-01

I need to convert this function of nodejs to be converted in php. Your help will be appreciated!

var captcha = sliderCaptcha({
  verify: function (arr, url) {
    var ret = false;
    fetch(url, {
      method: 'post',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(arr)
    }).then(function (result) {
      ret = result;
    });
    return ret;
  },
  remoteUrl: "api/Captcha"
});

CodePudding user response:

You can use the following function:

function sliderCaptcha(string $url) {
    // Create a stream
    $opts = [
        "http" => [
            "method" => "POST",
            "header" => "Accept: application/json\r\n" .
                "Content-Type: application/json\r\n"
        ]
    ];
    
    // DOCS: https://www.php.net/manual/en/function.stream-context-create.php
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    // DOCS: https://www.php.net/manual/en/function.file-get-contents.php
    $result = file_get_contents($url, false, $context);

    return $result;
}

$url = 'api/Captcha';

$captcha = sliderCaptcha($url);

CodePudding user response:

use curl for php

<?php
function sliderCaptcha($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$result = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($result == '200') {
    echo $result";
} else {
    echo "Fail!";
}
}

?>

  • Related