将JSON POST到API并使用PHP检索数据

时间:2016-02-19 08:35:30

标签: php json api curl

我尝试使用this API并使用所需参数对其进行POST。

使用此代码:

function postToService_php( $urlAbs, $data ) {
    $dataJSON = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlAbs);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($dataJSON))                                                                       
    );
    $resultJSON = curl_exec($ch);
    return $resultJSON;
}

$postData = array(
    'UserName' => '*username*',
    'Password' => '*password*',
    'Area' => 1
);

var_dump(postToService_php('http://e20prodwebapi.esmartapi.com/api/Authenticate', $postData));

这似乎给了字符串(0)""结果,我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

使用此:请考虑您必须为此站点使用SSL(HTTPS)

<?php

function postToService_php( $urlAbs, $data ) {
    $dataJSON = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlAbs);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1); //Remove this line if everything works okay.
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
    curl_setopt($ch,CURLOPT_POST, true);    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($dataJSON))                                                                       
    );
    $resultJSON = curl_exec($ch);

    return $resultJSON;
}

$postData = array(
    'UserName' => 'test',
    'Password' => 'test',
    'Area' => 1
);

var_dump(postToService_php('https://e20prodwebapi.esmartapi.com/api/Authenticate', $postData));

答案 1 :(得分:1)

在预感中我尝试了https并使用此代码

收到了预期的401响应
function postToService_php( $url, $data ) {
    $ch = curl_init();
    $data=json_encode( $data );

    curl_setopt($ch, CURLOPT_URL, $url );

    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $ch, CURLOPT_CAINFO, realpath( 'c:/wwwroot/cacert.pem' ) );
    }

    curl_setopt($ch, CURLOPT_POST, true );                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data );  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );                                                               
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',
        'Content-Length: ' . strlen( $data ))                                                                       
    );

    $result = curl_exec( $ch );
    $info=curl_getinfo( $ch );
    curl_close( $ch );

    return (object)array(
        'result'=>  $result,
        'info'  =>  $info
    );
}


$url='https://e20prodwebapi.esmartapi.com/api/Authenticate';
$postData = array(
    'UserName' => 'geronimo',
    'Password' => 'passthedoobie',
    'Area' => 1
);

print_r( postToService_php( $url, $postData ) );

答案 2 :(得分:1)

此API https是必需的。我没有账号所以无法测试。

https://e20prodwebapi.esmartapi.com/api/Authenticate