简体   繁体   中英

Trouble with php cURL library and REST POST request

I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:

cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=@-;type=application/json'  https://my.url.here

However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:

  $post = array(
    "exchangeInstance" => $json_string,
    "type" => "application/json",
  );
  $url = 'myurlhere';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

  $result = curl_exec($ch);
  $info = curl_getinfo($ch);

  var_dump($post);
  var_dump($result);
  echo $result;
  var_dump($info);

As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.

However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?

curl -F !== -d

$post = array(
    "exchangeInstance" => sprintf('@%s;type=application/json', $json_string),   
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM