I am new to PHP and am trying to figure out how to send some JSON data to a client server using a POST request in PHP. I have a username and password that I am supposed to use to send the information with basic authentication. Below is the code I have so far, I just put a sample for username, password, and url for security purposed. Can someone please help me figure out what I missed? Also can anyone explain or send me in the direction of documentation that can help me understand how this works? Google has been no help. And is there a way I can verify that the request actually went through?
//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);
New Version of Code:
//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>";
This new version still gives me an error when I run it in the browser saying "Fail. Please try Again." I think The issue is with the credentials.
LOG FILE:
* 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