douzhi2012 2017-02-14 09:01
浏览 23
已采纳

PHP curl和ISIC验证(REST api)。 我该如何工作?

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

  • 写回答

2条回答 默认 最新

  • drj26159 2017-02-14 09:28
    关注
    <?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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看