简体   繁体   中英

PHP curl and ISIC verification (REST api). How do I make this work?

I would like to verify whether someone has a valid ISIC card, I have written the following code for this Rest API ( http://nakoduj.to/_upload/project_files/2015-08-18-12-51-20_DM%20-%20Integration%20Manual.pdf ), but it doesn't work, and I have no idea why it doesn't.

$data = array(  "cardNumber" => "S123456789000A",
                        "cardholderName" => "John Doe");

$data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gts-dm.orchitech.net/api/verifications');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "testdm:testdm");

$result = curl_exec($ch);

if ($result === false) {
    $info = curl_getinfo($ch);
    curl_close($ch);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}

curl_close($ch);

I'm getting false for the $result always. And there is no additional information in $info.

Thank you in advance for your help

I recommend you to use Postman . Just install it and try to make request with it.
You'll see the result of your request and than can simply get the request code out.
You can see 'Code' on your top-right corner below 'save', press it and select your language. For your purposes it is PHP->cURL. And the result will look like this: 邮递员的例子
More about Postman here

<?php

$data = [
        "cardNumber" => "S123456789000A",
        "cardholderName" => "John Doe",
        ];
$data = json_encode($data);
$header = $request_headers = [
                "Content-Type: application/json"
            ];
$curl_options = [
        CURLOPT_URL =>'https://gts-dm.orchitech.net/api/verifications',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_AUTOREFERER => true,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_FILETIME => true,
        CURLOPT_FRESH_CONNECT => true,
        CURLOPT_USERPWD => "testdm:testdm"
];
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);

The request header was not sent causing http error code 415 UnsupportedMediaTypeHttpException Hence need to add request header, working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM