file_get_contents Default headers - php

I was wondering what is the default Content-Type header option when requesting a json string from an API.
I've noticed even if the requested Content-Type is application/json the response header Content-Type will be text/html
Does file_get_contents set the headers automatically based on what it is requesting or does it have a default one that is used always?

Does file_get_contents set the headers automatically based on what it is requesting or does it have a default one that is used always?
I think no defaults. For the context parameter you read below and see the 4-th example in docs:
A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.

Tested with Wireshark.
PHP 5.5.8 sends:
POST /path HTTP/1.0
Host: example.com
Content-Length: [appropriate number]
Content-Type: application/x-www-form-urlencoded
For the following context:
$options = array(
'http' => array(
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create($options);

Related

How to send raw data with curl GET in PHP?

I am developing REST API and while it is easy to set raw JSON data for request in cURL for POST
$payload = json_encode(array("user" => $data));
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
I cannot figure out how to send such data with GET requests.
Is there something like CURLOPT_GETFIELDS or CURLOPT_RAWDATA? The purpose of sending JSON with GET request is to pass in some params.
I do not wish to add formdata to the request, I wish to post JSON so that it can be parsed on the receiver.
Thanks!
EDIT:
based on comments I want to avoid confusion, so the resulting request should look like:
GET / HTTP/1.1
Host: 127.0.0.1:3000
Content-Type: application/json
Accept: application/json
Host: 127.0.0.1:3000
content-length: 13
Connection: keep-alive
cache-control: no-cache
{
"a": "b"
}
as you can see, GET request here has data and it is parsed and works perfectly by web server. How do I achieve this with cURL?
GET requests do not have a body, that's the whole idea: you're just getting something from the server, as opposed to posting something to it. From RFC 7231:
A payload within a GET request message has no defined semantics;
sending a payload body on a GET request might cause some existing
implementations to reject the request.
In other words, a GET request can have data, but it should not. From earlier in the spec, where GET is defined as a safe method:
Request methods are considered "safe" if their defined semantics are
essentially read-only; i.e., the client does not request, and does
not expect, any state change on the origin server as a result of
applying a safe method to a target resource.
...
Of the request methods defined by this specification, the GET, HEAD,
OPTIONS, and TRACE methods are defined to be safe.
If you really want to have JSON in your GET request (and send it to a reasonably implemented server resource) the only place it can go is in the URI as part of the query string. For GET requests I find using file_get_contents to be much easier than dealing with cURL.
<?php
$payload = json_encode(["user" => $data]);
$url_data = http_build_query([
"json" => $payload
]);
$url = "https://some.example/endpoint.php?" . $url_data;
$result = file_get_contents($url);
If you want to send it to an unreasonably implemented server resource, and violate the spirit of the HTTP RFCs, you could do this:
<?php
$url = "https://some.example/endpoint.php";
$payload = json_encode(["user" => $data]);
$ctx = stream_context_create(["http" => [
"header"=>"Content-Type: application/json",
"content"=>$payload
]]);
$result = file_get_contents($url, false, $ctx);
If you're determined to do this specifically with cURL, you might have luck with the CURLOPT_CUSTOMREQUEST option set to "GET" and CURLOPT_POSTDATA with your data.

Onedrive uploading fails with the Internal Server Error

I'm trying to create file on the OneDrive using REST API with PHP, but in the response I retrieve HTTP status code 500.
Code:
`
$url = $this->buildUrl(
'{folder_id}/files/{filename}?access_token={token}',
array(
'folder_id' => $folderId,
'filename' => $filename,
'token' => $this->getAccessToken(),
)
);
$response = wp_remote_request($url, array(
'body' => $content,
'method' => 'PUT',
));
`
Error message from the response body:
An error occurred while performing the action. Try again later.
What i'm doing wrong?
I just went through the same problem. It worked for me when I removed 'Content-type' line from request header.
If you are using PHP Curl to send request in wp_remote_request, you can remove 'Content-type' line from request header by calling something similar to this, before calling curl_exec:
curl_setopt($ci, CURLOPT_HTTPHEADER, array("Content-Type:"));
By adding the code above, the actual request header looks like this (note there is no 'Content-Type'):
PUT /v5.0/{folderId}/files/{filename}?access_token={accesstoken}
User-Agent: SOMEAGENT
Host: apis.live.net
Accept: */*
Expect: 100-continue
Content-Length: 29
FYI: I got a hint from here:
http://msdn.microsoft.com/en-us/library/dn631834.aspx
"For a PUT request, leave the Content-Type blank and put the contents of the file in the request body."
Hope it helps.

How to get request body without content-length?

Is there any way to retrieve a request body when a content-length header is not included in the request?
for example, I have this in /var/www/test.php:
print_r(apache_request_headers());
$data = file_get_contents('php://input');
var_dump($data);
And run this command:
curl -X POST -d test --header 'Content-Length: ' localhost/test.php
I see that there is no content-length header, and $data is empty. If I specify, for instance, a content-length of '3', I get an output for $data of 'tes'.
Is there any way to make php retrieve the request body, irrespective of the content-length header?
Is there any way to retrieve a request body when a content-length
header is not included in the request?
Yes. But you are including it in the request.
curl -d test localhost/test.php should return:
Array
(
[User-Agent] => curl/7.xx.x
[Host] => localhost
[Accept] => */*
[Content-Length] => 4
[Content-Type] => application/x-www-form-urlencoded
)
string(4) "test"
You can just omit sending the header.

How to make PHP Soap 1.1 call with non-default Content-Type

I need to make call to SOAP 1.1 web service in PHP. However, one of requirements in order to work is that I must send Content-Type=application/soap+xml. Now, I know that these are differences:
SOAP 1.2 -> Content-Type: application/soap+xml
SOAP 1.1 -> Content-Type: text/xml
I need to use SoapClient with WSDL to do this. However, I couldn't find how to set Content-Type after I set version to SoapClient to 1.1
Could someone provide example or code snippet?
Thank you!
There are a number of headers that are ignored when you try to set them in a stream_context, and will never be used.
Check the underlying C source file soap/php_http.c and search for "skip some predefined headers" for a list.
This is the reason some people report problems trying to set Host, Authorization, Content-Type and other headers in certain situations.
You can supply a stream context in the SoapClient options.
$ctx_opts = array(
'http' => array(
'header' => 'Content-Type: application/soap+xml'
)
);
$ctx = stream_context_create($ctx_opts);
$soapClient = new SoapClient('your.wsdl', array('stream_context' => $ctx));

Requesting JSON from PHP

I am trying to retrieve data from a server which usually returns it in XML, however I trying to request it in a JSON format (if requested correctly it will return the data in JSON).
$header = array(
'http' => array(
'header'=>"Content-type: application/json"
),
);
$response = file_get_contents($query, false, $header);
print_r($response);
This approach was taken from here. Currently the program does not return anything.
Does anyone spot any potential problems with this?
You need to set the HTTP Accept header to tell the server that you want it to give you JSON:
Accept: application/json
(assuming that the remote server is correctly implemented to read the header)
The Content-Type request header indicates the type of the payload that you are POSTing.
In your case, it does not apply, since you're sending a GET request.

Categories