Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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中使用带有凭据和基本身份验证的_POST发送JSON数据_Php_Authentication_Post_Http Post - Fatal编程技术网

如何在PHP中使用带有凭据和基本身份验证的_POST发送JSON数据

如何在PHP中使用带有凭据和基本身份验证的_POST发送JSON数据,php,authentication,post,http-post,Php,Authentication,Post,Http Post,我是PHP新手,正在尝试弄清楚如何使用PHP中的POST请求将一些JSON数据发送到客户机服务器。我有一个用户名和密码,我应该用它来发送带有基本身份验证的信息。 下面是我到目前为止的代码,为了安全起见,我只是放了一个用户名、密码和url的示例。有人能帮我找出我错过了什么吗?还有谁能向我解释一下或发送文档,帮助我理解这是如何工作的?谷歌对此毫无帮助。我有没有办法验证请求是否真的通过了 //Post request to post JSON file to client server $login

我是PHP新手,正在尝试弄清楚如何使用PHP中的POST请求将一些JSON数据发送到客户机服务器。我有一个用户名和密码,我应该用它来发送带有基本身份验证的信息。 下面是我到目前为止的代码,为了安全起见,我只是放了一个用户名、密码和url的示例。有人能帮我找出我错过了什么吗?还有谁能向我解释一下或发送文档,帮助我理解这是如何工作的?谷歌对此毫无帮助。我有没有办法验证请求是否真的通过了

//Post request to post JSON file to client server
$login = 'myusername';
$password = 'mypassword';
$url = 'https://myurl.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($encoded_array))                                                                       
);
$result = curl_exec($ch);
curl_close($ch);
新版本的代码:

//Post request to post JSON file to client server
$login = 'myusername';
$password = 'mypassword';
$url = 'https://myurl';
$ch = curl_init($url);
$encoded_array=json_encode($final_array);                                                                 
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_array);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($encoded_array))                                                                       
);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $login . ":" . $password);
$result = curl_exec($ch);
curl_close($ch);  
echo "<pre>$result</pre>";
//将JSON文件发布到客户端服务器的Post请求
$login='myusername';
$password='mypassword';
$url='1https://myurl';
$ch=curl\u init($url);
$encoded_array=json_encode($final_array);
curl_setopt($ch,CURLOPT_POSTFIELDS,$encoded_数组);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
'内容类型:application/json',
'内容长度:'.strlen($encoded_数组))
);
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch,CURLOPT_USERPWD,$login.:“$password);
$result=curl\u exec($ch);
卷曲关闭($ch);
回显“$result”;
当我在浏览器中运行此新版本时,仍然会出现一个错误,提示“失败。请重试”。我认为问题在于凭据

日志文件:

*正在尝试50.116.36.193:443。。。
*已连接到auth.da-dev.us(50.116.36.193)端口443(#0)
*阿尔卑斯山,提供h2
*ALPN,提供http/1.1
*已成功设置证书验证位置:
*CAfile:C:\xampp\apache\bin\curl-ca-bundle.crt
*卡帕斯:没有
*使用TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384的SSL连接
*ALPN,接受使用http/1.1的服务器
*服务器证书:
*主题:CN=da-dev.us
*开始日期:3月15日13:50:02 2021 GMT
*过期日期:6月13日13:50:02 2021 GMT
*subjectAltName:主机“auth.da-dev.us”与证书的“auth.da-dev.us”匹配
*发行人:C=美国;O=让我们加密;CN=R3
*SSL证书验证正常。
*使用用户“masha”的Basic进行服务器身份验证。mir2015@gmail.com'
>POST/devtest1 HTTP/1.1
主持人:auth.da-dev.us
授权:基本BWFZAGEUEBWLYMJAXNUBNBWFPBC5JB206TWFYMTXOTKXLG==
接受:*/*
内容类型:application/json
内容长度:37
*上传完全发送:37个字节中的37个
*将捆绑包标记为不支持多用途
try要改变的一件事是
curl\u setopt($ch,CURLOPT\u POST,1)用于调试帮助-添加
curl\u setopt($curlhandle,CURLOPT\u VERBOSE,true)
(from)@Nigel Ren,那篇CURLOPT_POST我应该添加那一行还是用那一行替换CURLOPT_POST字段?更多替换
CURLOPT_CUSTOMREQUEST
。使用
CURLOPT_VERBOSE
选项尝试一下,这也可能会为您提供有关问题的更多信息。
*   Trying 50.116.36.193:443...
* Connected to auth.da-dev.us (50.116.36.193) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: C:\xampp\apache\bin\curl-ca-bundle.crt
*  CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=da-dev.us
*  start date: Mar 15 13:50:02 2021 GMT
*  expire date: Jun 13 13:50:02 2021 GMT
*  subjectAltName: host "auth.da-dev.us" matched cert's "auth.da-dev.us"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
* Server auth using Basic with user 'masha.mir2015@gmail.com'
> POST /devtest1 HTTP/1.1
Host: auth.da-dev.us
Authorization: Basic bWFzaGEubWlyMjAxNUBnbWFpbC5jb206TWFyMTMxOTkxLg==
Accept: */*
Content-Type: application/json
Content-Length: 37

* upload completely sent off: 37 out of 37 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 BAD REQUEST
< Server: nginx/1.12.1
< Date: Mon, 10 May 2021 15:23:39 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 24
< Connection: keep-alive
< 
* Connection #0 to host auth.da-dev.us left intact