Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON发布到API并使用PHP检索数据_Php_Json_Api_Curl - Fatal编程技术网

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

将JSON发布到API并使用PHP检索数据,php,json,api,curl,Php,Json,Api,Curl,我正在尝试使用和发布所需的参数 使用此代码: function postToService_php( $urlAbs, $data ) { $dataJSON = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $urlAbs); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "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)“因此,我在这里做了什么错误?

使用这个:考虑你必须使用SSL这个站点(HTTPS)


<代码> 使用此:考虑您必须为这个站点使用HTL(http)


凭直觉,我尝试了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 ) );

凭直觉,我尝试了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 ) );

此API是必需的。我没有帐户,所以无法测试

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

此API是必需的。我没有帐户,所以无法测试

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

作为测试,尝试添加
$info=curl\u getinfo($ch);返回$info编码到curl函数,然后查看它显示了什么。对我来说(没有用户名/密码),我得到了403个响应,我似乎得到了相同的响应,用户名和密码。编辑:根据“返回200-OK和用户信息,401未经授权或410消失,如果用户不在该区域(网络或电源)。”如果用户不存在,它应该给410,对吗?我看不到注册的方法,所以无法进一步测试-虽然使用https确实返回401WeSome,https做到了!谢谢:)作为测试,尝试添加
$info=curl\u getinfo($ch);返回$info编码到curl函数,然后查看它显示了什么。对我来说(没有用户名/密码),我得到了403个响应,我似乎得到了相同的响应,用户名和密码。编辑:根据“返回200-OK和用户信息,401未经授权或410消失,如果用户不在该区域(网络或电源)。”如果用户不存在,它应该给410,对吗?我看不到注册的方法,所以无法进一步测试-虽然使用https确实返回401WeSome,https做到了!谢谢:)