Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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 cURL库和REST POST请求存在问题_Php_Rest_Post_Curl - Fatal编程技术网

php cURL库和REST POST请求存在问题

php cURL库和REST POST请求存在问题,php,rest,post,curl,Php,Rest,Post,Curl,在我发布的API发布了他们API的最终版本之后,我遇到了REST POST请求的问题。它可以正常工作,而且我被告知,在新版本中,服务器对“application/json”类型的要求更严格。以下cli curl命令工作正常: cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=@-;type=application/json' https://my.url.here 但是,我需要在代码中执行此操作。使用php curl库

在我发布的API发布了他们API的最终版本之后,我遇到了REST POST请求的问题。它可以正常工作,而且我被告知,在新版本中,服务器对“application/json”类型的要求更严格。以下cli curl命令工作正常:

cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=@-;type=application/json'  https://my.url.here
但是,我需要在代码中执行此操作。使用php curl库,我得到了一个简单的测试脚本,如下所示:

  $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);
在阅读文档时,如果我将数组作为CURLOPT_POSTFIELDS传递,那么标题中的内容类型应该自动设置为“multipart/form”,然后我将元素传递的类型设置为数组中的“application/json”


但是,api没有收到我的POST请求。我从他们那里得到一个错误,清楚地表明他们正在接收GET请求。这怎么可能呢?我错过了什么?

curl
-F
!=<代码>-d

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

你得到了followlocation=true。如果服务器正在执行重定向,则重定向到url的内容将通过GET获取,即使您是从一篇文章开始的。关掉它,看看你能得到什么。这当然符合我正在处理的内容,但我找不到任何关于它的文档。有线索吗?我感觉我在a点被验证,并被重定向到B点。我被重定向到一个特定的端口,但我使用的url与CLI命令中相同。这非常有趣。感谢上帝给了聪明人。我肯定这是问题的一部分,但目前并非全部。当我知道更多的时候,我会发回的-谢谢!所以,除非我传递一个文件的路径,否则这不会真正起作用。否则,它将发送一个附加有@符号的字符串。。。我没有找到一种方法来设置单个mime多部分头的内容类型,而不向其传递文件。在我看来,应该有更好的方法,而不是将字符串写入临时文件,然后将其发送到curl库。。。有什么想法吗?