json - Trouble POSTing using cURL, PHP, and Basic Authentication -
i made similar post last week, can found here. having problems authentication, believe i've solved. error message different, @ least.
i'm trying recreate successful post made our vendor via postman executing curl commands in php.
here example curl command documentation:
curl -i -k -u '<api_key>': -xpost --data-urlencode assessment_data@/path/to/test/file.json "https://<your_subdomain>.vendor.org/api/v1/import"; echo ""
this php code, not work:
<?php session_start(); include('../connect.php'); include('../functions.php'); function curlpost($url, $headers, $username, $password, $post) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_cainfo, 'certificate.pem'); curl_setopt($ch, curlopt_ssl_verifypeer, true); curl_setopt($ch, curlopt_ssl_verifyhost, 2); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlinfo_header_out, true); curl_setopt($ch, curlopt_httpauth, curlauth_basic); curl_setopt($ch, curlopt_userpwd, $username . ":" . $password); curl_setopt($ch, curlopt_timeout, 30); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $post); curl_setopt($ch, curlopt_returntransfer, true); echo '<pre>'; echo curl_exec($ch) . '<br><br>'; echo print_r(curl_getinfo($ch)) . '<br><br>'; echo curl_error($ch) . '<br><br>'; echo '</pre>'; curl_close($ch); } $data = 'json_object={"json_key":"json_value"}'; curlpost('https://company.vendor.org/api/v1/import', ['content-type: multipart/form-data'], 'api-key', '', $data );
and instead produces output:
{"success":false,"errors":["500 internal server error"],"warnings":[],"info":[],"meta":[],"results":[]} array ( [url] => https://company.vendor.org/api/v1/import [content_type] => application/json; charset=utf-8 [http_code] => 500 [header_size] => 547 [request_size] => 248 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.586493 [namelookup_time] => 0.135777 [connect_time] => 0.177182 [pretransfer_time] => 0.286958 [size_upload] => 1999 [size_download] => 103 [speed_download] => 64 [speed_upload] => 1260 [download_content_length] => 103 [upload_content_length] => 1999 [starttransfer_time] => 0.345878 [redirect_time] => 0 [redirect_url] => [primary_ip] => xx.xxx.xx.xxx [certinfo] => array ( 'this array empty, didn't delete anything.' ) [primary_port] => 443 [local_ip] => xxx.xxx.xx.xxx [local_port] => 60532 [request_header] => post /api/v1/import http/1.1 authorization: basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx host: company.vendor.org accept: */* content-type: multipart/form-data content-length: 1999 expect: 100-continue ) 1
for measure, here screenshot of successful post using postman (it's censored little differently, i'm passing in exact same information):
their documentation contains example json object, save file , send form-data. i've managed recreate object live data, output formatted string, , send value instead. works in postman, not in php, suspect problem lies there somehow. curlopt_postfields requires string (tried array - got mad), , i've tried formatting every way can think of. appreciated.
Comments
Post a Comment