0

I made a similar post last week, which can be found here. I was having problems with authentication, which I believe I've solved. The error message is different, at least.

I'm trying to recreate a successful POST I made to our vendor via POSTman by executing cURL commands in PHP.

Here is the example cURL command from their 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 is my PHP code, which does 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 this 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 is actually empty, I 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 good measure, here is a screenshot of a successful POST using POSTman (It's censored a little differently, but I'm passing in the exact same information):

enter image description here

Their documentation contains an example JSON object, which they save as a file and send as form-data. I've managed to recreate the object with live data, output it to a properly formatted string, and send that as the value instead. This works in POSTman, but not in my PHP, so I suspect the problem lies there somehow. CURLOPT_POSTFIELDS requires a string (tried an array - it got mad), and I've tried formatting it every way I can think of. Any help would be greatly appreciated.

user3630824
  • 465
  • 2
  • 7
  • 14
  • Turn on FireBug in Firefox or hit ctrl-shift-i in Chrome and actually look at the exact headers/data being sent in the request from POSTMan. You can even copy out the cURL request and convert it from the CLI version to what PHP uses (right click the URL in the Net panel). Try posting those headers here if you're not seeing the issue. – BA_Webimax Jun 29 '15 at 14:59
  • @BA_Webimax These are the headers directly from POSTman: Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Authorization:Basic xxxxxxxxxxxxxxxxxxxxx Cache-Control:no-cache Connection:keep-alive Content-Length:1786 Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBh3OUKXCFL7H96DQ Cookie:BackendSessionID=814ce539e86dad2349de57316459df62; __uvt=; spinnerLeft=826px; spinnerTop=-4px CSP:active DNT:1 – user3630824 Jun 29 '15 at 15:52
  • Host:company.vendor.org Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36 – user3630824 Jun 29 '15 at 15:54
  • -------------- and these are the headers i'm sending w/ PHP: Host: company.vendor.org Accept: */* Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Accept-Encoding: gzip, deflate Accept-Language:en-US,en;q=0.8 Connection: keep-alive Content-Length: 1495 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------2322a885f91a – user3630824 Jun 29 '15 at 15:55
  • I'm back to getting authentication issues – user3630824 Jun 29 '15 at 15:55

0 Answers0