2

Using curl request from Laravel.

    $path =  storage_path('app/letters/letter.pdf');
    $post = '@' . $path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://sign.zoho.com/api/v1/requests');
    $authorization = 'Authorization: Bearer ' . $zohoAccessToken;
    curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    $entriesData = ['data' => [
        'requests' => [
            'request_name' => "NDA ",
            'actions' => [
                'recipient_name' => 'test',
                'recipient_email' => $mail,
                'action_type' => 'sign',
                'verify_recipient' => false,
            ],
            'is_sequential' => false,
        ]
    ]];
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($entriesData));
    $output = curl_exec($ch)

And getting the error response: "message": "Extra key found", "status": "failure", "code": 9015,

I'm using this code to get access token and it works well

        $data = [
            'refresh_token' => $refreshToken,
            'client_id' => $clientId,
            'client_secret' => $secret,
            'grant_type' => 'refresh_token'
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://accounts.zoho.com/oauth/v2/token');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
rati_geo
  • 63
  • 5
  • Why do you set `CURLOPT_POSTFIELDS` twice. – Indra Kumar S Jun 10 '21 at 10:27
  • @IndraKumarS I need to pass 2 parameters : path to file and description. Ok i merged that two parameter into $entriesData array but got same error – rati_geo Jun 10 '21 at 10:28
  • “Extra key found” sounds like you are sending _more_ data, then this endpoint expects. Where is the documentation for the particular action you are trying to undertake here located? – CBroe Jun 10 '21 at 10:33
  • @CBroe https://www.zoho.com/sign/api/#document-management – rati_geo Jun 10 '21 at 10:34

1 Answers1

4

Based on the API documentation and this post, I believe that this should look something like:

$path =  storage_path('app/letters/letter.pdf');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sign.zoho.com/api/v1/requests');
$authorization = 'Authorization: Bearer ' . $zohoAccessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  'data' => json_encode([
    'requests' => [
      'request_name' => "NDA ",
      'actions' => [
        'recipient_name' => 'test',
        'recipient_email' => $mail,
        'action_type' => 'sign',
        'verify_recipient' => false,
      ],  
      'is_sequential' => false,
    ]   
  ]), 
  'file' => curl_file_create($path)
];
$output = curl_exec($ch);

With that, it seems to produce approximately the same payload as a CURL command line invocation as per the API documentation.

The main source of confusion for me was having data being the key for the field, and the corresponding value being JSON encoded.

msbit
  • 4,152
  • 2
  • 9
  • 22