php curl post不发送数据

时间:2015-07-06 18:59:59

标签: php curl

我正在尝试发送POST数据:

$url ="https://server:510/1/auth/";

$data = array(
      "USERNAME" => "user",
      "PASSWORD" => "password"
 );

$str_data = json_encode($data);

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
    'Content-Length: ' . strlen($str_data))
); 
curl_setopt( $ch, CURLOPT_POSTFIELDS, $str_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch,CURLOPT_POSTREDIR,3);
$response = curl_exec( $ch );
print_r($response);

并且服务器响应是(详细的结果): "您还没有发送任何凭据"

> POST /1/auth/ HTTP/1.1
Host: server:port
Accept: */*
Content-Length: 39
Content-Type: application/x-www-form-urlencoded

< HTTP/1.1 200 OK
< Date: Mon, 06 Jul 2015 18:53:52 GMT
< Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i mod_wsgi/3.5 Python/2.7.8
< Content-Length: 73
< Content-Type: application/json
< 

好像我没有发送帖子数据。我错过了什么?

我注意到在chrome调试器中,在网络下,请求是作为GET发送的....

以下是来自服务器供应商的文档:

例如,假设您的服务器托管在10.211.55.3。要请求会话令牌,请在以下URL中提供您的用户名和密码作为POSTING变量(请注意使用/ 1 /,即API版本号):

https://10.211.55.3/1/auth
USERNAME  = 'api@domain.com'
PASSWORD  = 'Tester01'

就是这样:(

1 个答案:

答案 0 :(得分:0)

$url ="https://server:510/1/auth/";
$data = array(
      "username" => "username",
      "password" => "password"
 );

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override:'POST','Content-Type:application/x-www-form-urlencoded','Content-Length: ' .     strlen(http_build_query($data))));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec( $ch );

这很有用。

相关问题