使用POST XML文件调用Fedena API curl会返回400错误

时间:2014-05-23 07:40:51

标签: php xml curl

我正在尝试将学生数据发布到fedena API。因此,如果API调用成功,那么它将添加一个学生。但是,当我尝试此代码时,它返回400错误。

我的代码:

<?php 

session_start();
function garland_curlapi_call($url,$headers = NULL,$method = NULL,$data = NULL){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if($headers != "")
    {
        //curl_setopt($ch, CURLOPT_HTTPHEADER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    }
    if($data != ""){ $data = http_build_query($data); }
    if($method == "POST")
    {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POST, count($data));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $result = curl_exec($ch); // echo $result;

    $curl_info = curl_getinfo($ch);

    //print_r($curl_info); exit; 
    $content_type = explode(";",$curl_info['content_type']);


    if($content_type[0] == "application/json"){
        $result_data = json_decode($result,TRUE);

    }
    elseif($content_type[0] == "application/xml"){
        $xml = simplexml_load_string($result);
        $json = json_encode($xml);
        $result_data = json_decode($json,TRUE);

    }
    else {
        $result_data = $result;
    }
    if (curl_errno($ch))
    {
        return array("status" => "error" , "data" => curl_error($ch) ,'curn_info' => $curl_info);
    }
    else
    {
        curl_close($ch);
        return array("status" => "success" , "data" => $result_data ,'curn_info' => $curl_info);
    }
}


function garland_get_fedena_accesstoken($new = NULL)
{
    if($_SESSION['fedena_access_key'] != "" && $new == "")
    {   
         return $_SESSION['fedena_access_key'];

    }

}




$xml = "http://mywebsite.com/test/student.xml";

$access_token = garland_get_fedena_accesstoken();
$url = "http://test.sandbox.fedena.com/api/students";
$headers = array('Authorization: Token token="'.$access_token.'"');
$method = "POST";
$data = array("student" => $xml);
$result = garland_curlapi_call($url, $headers, $method, $data);


print_r($result); 
?>

输出:

Array
(
    [status] => success
    [data] => Array
        (
            [error] => Array
                (
                    [invalid_request_error] => Invalid request.
                )

        )

    [curn_info] => Array
        (
            [url] => http://test.sandbox.fedena.com/api/students
            [content_type] => application/xml; charset=utf-8
            [http_code] => 400
            [header_size] => 606
            [request_size] => 305
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 0.540113
            [namelookup_time] => 0.012328
            [connect_time] => 0.265829
            [pretransfer_time] => 0.265907
            [size_upload] => 64
            [size_download] => 135
            [speed_download] => 249
            [speed_upload] => 118
            [download_content_length] => 135
            [upload_content_length] => 64
            [starttransfer_time] => 0.540075
            [redirect_time] => 0
            [redirect_url] => 
            [primary_ip] => 54.254.105.83
            [certinfo] => Array
                (
                )

            [primary_port] => 80
            [local_ip] => 10.0.0.8
            [local_port] => 47643
        )

)

用于创建学生的API API的FEDENA文档:

POST / api / students /

这是创建在数据库中的学生对象。

需要参数:

{'student':'STUDENT DETAIL XML FILE',’student_photo’:’<PHOTO FILE>’}    

N.B:可以从/api/students/student_structure下载正确的XML格式。突出显示所需的参数。参数学生是必要的。可以跳过其他参数。

结果:

如果成功创建,响应将类似于以下

    <student_detail> 
 <student> 
 <student_name>DEBANJAN SEN GUPTA</student_name> 
 <batch_name>A ­ TEST</batch_name>  
<admission_date>2013­04­05</admission_date> 
 <blood_group>A+</blood_group> 
 <gender>m</gender>  
<nationality>India</nationality>
  <language>Bengali</language>
  <category/>  <religion>Hinduism</religion>  
<address>HATIBAGAN BEADON STREET</address>  
<city>KOLKATA</city>  
<state>WEST BENGAL</state>
  <country>India</country> 
 <phone>03325436969</phone>  
<mobile>9620296538</mobile>  
<email>dave4u901@gmail.com</email> 
 <photo/> 
 <immediate_contact/> 
 <student_additional_details>  <additional_field>  <name>Field 2</name>  <value>TEST 2</value>  </additional_field>  <additional_field>  <name>Field 1</name>  <value>TEST 1</value>  </additional_field>  </student_additional_details>  </student>  
</student_detail>  

否则如果发生错误,它将像这样呈现错误

<?xml version="1.0" encoding="UTF­8"?>  
    <errors>    
    <error>Admission date can't be blank</error>  
       <error>First name can't be blank</error> 
       <error>Batch can't be blank</error>
        <error>Date of birth can't be blank</error>  
    <error>Gender can't be blank</error>
</errors>

0 个答案:

没有答案