-1

EDIT ........

This is NOT the question as posted. The context has been changed by @miken32, thank you for that. It appears this was done so the same person could say this is a duplicate of a "similar" question, but is not. It is reasonable to ensure one is doing Part A correctly before trying to do part B , which may also lead to Part C.

The suggested "duplicte" does not contain a full answer as was the same problem I originally expressesed in my original post. that has been edited to death. Not one example shown that I saw there has a POST command in it.The answers are incomplete. It is not the reply for a cURL POST , and the structure of the code is somewhat different. If I were and expert I would not be asking! Sure I can go and get a reply but need the reply for my POST.

There is no logic in removing half or more of the text in my original question regardless of how many points you have or make. in fact what was left has completely changed the question and does not help me to ensure part A is correct before moving on to part B. There is even a reply as to the header being incorrect but that portion of post has now been removed. Obviously that post was made before the edit. What now remains is a seemingly irrelevant reply . Oh and othanks again.

I ask that whoever does these , be a little more considerate of the poster rather than making the question what you WANT it to be.

I think I will be posting less on stackanything or serveranything and more in other places. This is not the first time and it is completely disrespectful to the original post!

I hoped to find some guidance here and only got frustration.

EDIT........................................

I am. trying to upload a file to a page that responds with an HTTP 201 Created message and a Location header. I need to get the value of that header.

$request = curl_init('http://localhost:80/eSCL/ScanJobs');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('ScanSettings.xml')
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);

This works but does not get me the header value I want.

user178167
  • 43
  • 2
  • 7
  • Possible duplicate of [Get Header from PHP cURL response](https://stackoverflow.com/questions/41978957/get-header-from-php-curl-response) – miken32 May 23 '19 at 03:12
  • Please limit yourself to one question at a time. I've edited it to be less broad. – miken32 May 23 '19 at 03:14

1 Answers1

0

Your header-function uses not the correct syntax for its arguments. The second argument is $replace (bool, defaults to true), which indicates whether an existing header should be overwritten

header('HTTP/1.1 201', 'Created');

Should be:

header('HTTP/1.1 201 Created');

If you want to read the headers with curl you have to activate that option:

curl_setopt($request, CURLOPT_HEADER, 1);

Then you have to filter the headers from the returned data after calling curl_exec:

$response = curl_exec($request); // don't echo directly, we need that data filtered!

$header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Jan
  • 2,853
  • 2
  • 21
  • 26
  • first let me say that my original question has since been edited by I have no idea who. That removed the heart of the question. This site gets more frustrating to use every day, @Jan I need the 201 Created and the URL . You say that the default is to overwrite and give three examples with no explanation of the difference. – user178167 May 23 '19 at 03:24
  • @miken32 is there a reso you removed MOST of the text in my question? You did that only t say it is a "possible duplicate"? More points for you! To hell with my question. – user178167 May 23 '19 at 03:29
  • The manual says "replace header of a similar type" so I guess we do not know what is a "similar type" as there is no further explanation. – user178167 May 23 '19 at 04:04
  • Similar type means any header with the same "name"/beginning. In your case it is the http-status-header that includes the http-response-code. Another way to set that response-code is to use [http_response_code(*201*)](https://www.php.net/manual/de/function.http-response-code.php). You should validate if/that the correct headers are sent. You can use any any web-developer-console or even most of the shells/terminals for that. The code-suggestions i posted regarding `curl`, can also be used to get the headers. So you can identify on which side (server/client) the source of your problem is. – Jan May 23 '19 at 08:16