Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Php 通过身份验证获取请求_Php_Json_Rest - Fatal编程技术网

Php 通过身份验证获取请求

Php 通过身份验证获取请求,php,json,rest,Php,Json,Rest,我有一些代码,可以使用身份验证将JSON发布到API。我需要使用身份验证,但大多数示例不提供此信息 职位 得到(不工作) 我错过了什么?我也希望保持这种风格,而不是使用 编辑: 作为参考,这里是可以工作的命令行版本 curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget" 您需要使用C

我有一些代码,可以使用身份验证将JSON发布到API。我需要使用身份验证,但大多数示例不提供此信息

职位

得到(不工作)

我错过了什么?我也希望保持这种风格,而不是使用

编辑: 作为参考,这里是可以工作的命令行版本

curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget"

您需要使用
CURLOPT\u HTTPAUTH

使用CURLOPT_HTTPAUTH指定HTTP的身份验证方法 基于连接

您需要添加:

curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
例如:

public function call(array $data)
{
    $payload = json_encode($data['payload']);

    // Initialise the session
    $curl = curl_init();

    // Set API URL
    curl_setopt($curl, CURLOPT_URL, $data['url']);
    // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // Include the header in the output
    curl_setopt($curl, CURLOPT_HEADER, true);
    // Authentication
    curl_setopt($curl_handle, CURLOPT_USERPWD, 'your_username:and_password');
    // Set request method
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $data['method']);

    if ($data['method'] == 'POST') {
        // Do a regular HTTP POST
        curl_setopt($curl, CURLOPT_POST, true);
    }

    if ($data['method'] == 'POST' || $data['method'] == 'PUT') {
        // The full data to post in a HTTP POST operation
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($payload)
        ));
    }

    // Execute the request
    $output = curl_exec($curl);
    // Close curl resource to free up system resources
    curl_close($curl);

    // If connection failed
    if (! $output) {
        return 'Curl connection failure.';
    }

    // Output the profile information, includes the header
    return $output;
}

因此,POST需要
CURLOPT\u-HTTPHEADER=>array(“内容类型:application/json”),
,而GET需要
CURLOPT\u-HTTPHEADER=>array(“Accept:application/json”),
现在就可以阅读了

public function getTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Accept: application/json" ),
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}

试过这个,我想我错过了别的东西
public function call(array $data)
{
    $payload = json_encode($data['payload']);

    // Initialise the session
    $curl = curl_init();

    // Set API URL
    curl_setopt($curl, CURLOPT_URL, $data['url']);
    // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // Include the header in the output
    curl_setopt($curl, CURLOPT_HEADER, true);
    // Authentication
    curl_setopt($curl_handle, CURLOPT_USERPWD, 'your_username:and_password');
    // Set request method
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $data['method']);

    if ($data['method'] == 'POST') {
        // Do a regular HTTP POST
        curl_setopt($curl, CURLOPT_POST, true);
    }

    if ($data['method'] == 'POST' || $data['method'] == 'PUT') {
        // The full data to post in a HTTP POST operation
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($payload)
        ));
    }

    // Execute the request
    $output = curl_exec($curl);
    // Close curl resource to free up system resources
    curl_close($curl);

    // If connection failed
    if (! $output) {
        return 'Curl connection failure.';
    }

    // Output the profile information, includes the header
    return $output;
}
public function getTest() {
    $username    = "user";
    $password    = "pass";
    $json_url    = "http://api.of.site.com";

    $ch      = curl_init( $json_url );
    $options = array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD        => "{$username}:{$password}",
        CURLOPT_HTTPHEADER     => array( "Accept: application/json" ),
    );
    curl_setopt_array( $ch, $options );

    $result = curl_exec( $ch );
}