0

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
masha
  • 11
  • 3
  • One thing to change try is `curl_setopt($ch, CURLOPT_POST, 1);` – Nigel Ren May 10 '21 at 14:48
  • For debugging help - add `curl_setopt($curlhandle, CURLOPT_VERBOSE, true);` (from https://stackoverflow.com/questions/3757071/php-debugging-curl) – Nigel Ren May 10 '21 at 14:50
  • @Nigel Ren, that CURLOPT_POST should I add that line or replace the CURLOPT_POSTFIELD with that? – masha May 10 '21 at 15:05
  • More replacing `CURLOPT_CUSTOMREQUEST`. – Nigel Ren May 10 '21 at 15:08
  • Try it with the `CURLOPT_VERBOSE` option, this may also give you some more information about the problems. – Nigel Ren May 10 '21 at 15:09
  • I do not have a CURLOPT_CUSTOMREQUEST Line in my code. I just tried adding the line you suggested in the first comment to my code, but no change, I am still getting the same "Failed. Please try again." I have posted a copy of the revised code to my questions before I added the line you suggested. I'm sorry for all the questions, I am trying to understand how to use POST with username and password as I have never had the need to do so before. – masha May 10 '21 at 15:12
  • The CURLOPT_VERBOSE did not provide me with any information. unless I am doing it wrong and reading it in the wrong place. – masha May 10 '21 at 15:14
  • You could try with `curl_setopt($c, CURLOPT_STDERR, fopen('curl.log', 'w+'));` which might help. You WILL need `curl_setopt($ch, CURLOPT_POST, 1);` in the last code sample. – Nigel Ren May 10 '21 at 15:16
  • I just edited the code in the question so you can see what I have at this moment. I really appreciate you taking the time to help! I added the CURLOPT_POST and the CURLOPT_VERBOSE. – masha May 10 '21 at 15:21
  • Try with the `CURLOPT_STDERR` setting as well. – Nigel Ren May 10 '21 at 15:22
  • I just did and it still shows me the message "Failed. Please try again." in my web browser – masha May 10 '21 at 15:24
  • Did it create a file called curl.log? – Nigel Ren May 10 '21 at 15:24
  • Yes it did..I have no idea what it means tho, There is a line in there that says HTTP bad request, idk if that means anything. Should I add the result of that file to my question so you can see what i'm seeing? – masha May 10 '21 at 15:25
  • Is there anything in there which may hint at an error? – Nigel Ren May 10 '21 at 15:26
  • I think so, I just added that info to my last comment. – masha May 10 '21 at 15:27
  • It would help to see the log file. – Nigel Ren May 10 '21 at 15:28
  • I just added a copy of it to my question. Again, Thank you so much for taking the time to help me with this!! I never would have been able to figure that out on my own! You are a life saver! – masha May 10 '21 at 15:31
  • Not sure if it will change anything, but try `$ch = curl_init(urlencode($url));` – Nigel Ren May 10 '21 at 15:36
  • I added that and the log file now shows " * Could not resolve host: https%3A%2F%2Fauth.da-dev.us%2Fdevtest1 * Closing connection 0", and the webpage no longer shows the failed error. So I think it did something... – masha May 10 '21 at 15:40
  • Looks as though you need to remove that bit then - the error basically sounds like you are sending some data which the system doesn't like. Not sure what that is, but you need to check an example of what you know works and perhaps see if `$encoded_array` looks like the working example. – Nigel Ren May 10 '21 at 15:44
  • The data im sending is an encoded array that is supposed to be in JSON format that has names of two employees and how much they were paid. Before making the post request I had encoded the data into a JSON object and printed it to the browser and it worked fine. I also don't have an example to try it with..... – masha May 10 '21 at 15:53
  • Not sure, I've reopened the question to see if anyone else can help. – Nigel Ren May 10 '21 at 15:59

0 Answers0