php curl data raw

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

I cannot figure out how to send such data with GET requests.

I do not wish to add formdata to the request, I wish to post JSON so that it can be parsed on the receiver.

based on comments I want to avoid confusion, so the resulting request should look like:

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?

1 Answer 1

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.

If you want to send it to an unreasonably implemented server resource, and violate the spirit of the HTTP RFCs, you could do this:

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.

Источник

How can I send a raw JSON using PHP curl with the content type «application/x-www-form-urlencoded»?

Let me explain:

I’m communicating with a webserver that accepts HTTP POST requests with a JSON object as the body of the request where normally we are used to seeing HTTP query parameters.

In my situation, I need to send a request with the following content-type

The body must be raw JSON.

So, there are many possibilities. I tried the following:

I also tried to escape the json_encode() :

If the server was able to parse html parameters I could just do this:

However, that is not the case and I need a workaround.

2 Answers 2

Usually the application/x-www-form-urlencoded requires a key-value paired parameters for HTTP post. So it’s very hard to suggest anything to you without seeing a sample POST data format. As per the document, you must place the URLencoded data with a variable. For example your JSON should go like this.

You can try sending the data without any key parameter, and it should not work

php curl data raw. Смотреть фото php curl data raw. Смотреть картинку php curl data raw. Картинка про php curl data raw. Фото php curl data raw

I’m not entirely sure I understand your question, so I’m going to answer two different versions.

Send JSON data, but with an (inaccurate) application/x-www-form-urlencoded Content Type

I don’t know why you’d want to do this, but if you do, it should be fairly simple.

Bear in mind that you are, here, sending deliberately inaccurate data to the server. You’re sending JSON, but calling it urlencoded. You probably don’t want to do this; if, for some reason, you do need to do this, you’d probably be better off fixing whatever the real problem is, rather than using this hacky workaround.

Send urlencoded JSON data

This is an odd setup, but accurate. At least you wouldn’t be lying in your HTTP headers.

Basically the same as above, but add this:

What you probably really should do: send JSON as JSON.

Источник

I’m using CURL to upload files to a service.

after that, I’m using CURLOPT_POSTFIELDS with the file’s full path. Is there a way to send the file content directly, without saving it on my server, as if I saved it?

Or is there a way to upload a Photo from a flash app to facebook album, without saving it on the server?

1 Answer 1

If you are uploading data you might consider using the file upload mechanism in PHP http://php.net/manual/en/features.file-upload.php It automatically handls file upload PHP.

If you want to redirect the upload to another (third party service) without needing to be in the chain of commands (i.e. user->3rd party server), you might want to look into AJAX. AFAIK when you upload a file using PHP/forms the file will be uploaded to your PHP temp directory and there is no way to prevent this because:
1. To access the file it needs to be on the server (PHP is server execute meaning it can not execute on the user side)
2. I do not believe any user will want you to access their files on their computer nor will you be able to do so(Firewall, AV), if that were to happen it will be a major security issue

As I said above, what you want to look into is AJAX (I used jquery and their AJAX methods are very simple). Because AJAX is user execute javascript it can run on the machine and initiate a connection to any URL. This way you can directly access the service without submitting the file to your server.

Источник

Reading POST data in PHP from cUrl

I am using cUrl in PHP to request from some external service.

Interesting enough, the server is responding with raw «multipart/form-data» instead of binary file data.

My website is using a shared hosting, therefore PECL HTTP is not an option.

Is there a way to parse this data with PHP?

EDIT: I tried piping the response to a localhost HTTP request, but the respond data is likely to exceed the allowed memory size in PHP process. Expending mem limit is not very practical, this action also dramatically reduces the server performance dramatically.

If there is no alternatives to the original question, you may suggest a way to handle very large POST requests, along with XML parsing, in terms of streams in PHP.

I know this would be hard, please comment. I am open for discussions.

php curl data raw. Смотреть фото php curl data raw. Смотреть картинку php curl data raw. Картинка про php curl data raw. Фото php curl data raw

5 Answers 5

if you need the zip file from the response I guess you could just write a tmp file to save the curl response to, and stream that as a workaround: Never tried that with multipart curls, but I guess it should work.

if you do NOT need anything but the xml part of the response you might want to disable headers

and add option to only accept xml as a response

A Shot in the dark. can you test with these curlopt settings to see if modifiying these help anything

[EDIT II]

This is just a try, as mentioned I cannot get my curled pages to respond with a multipart form data. So be gentle with me here 😉

and of course set the proper curlopts

[EDIT III]

Say we want curl to write all incoming data directly to another (outgoing) stream, in this case a socket connection

I’m not sure if it is as easy as this:

else we will have to use our known write and header functions with just a little trick

Источник

When making HTTP raw POST request multipart/form-data with cURL PHP, how to properly set the Content-Length header?

I have just a question about the way I need to set the Content-Length header using curl in PHP to make multipart/form-data POST HTTP requests.

For example, I have a PHP script which makes a curl POST uploading an image to the server, the content of the HTTP entity body of the request would look like this:

I do something like this:

But then, which value should I set for the Content-Length header? I know, it is the number of bytes of the entity body, but how should I handle the «\r\n»? I mean, if I do strlen(«\r\n») I get 2 as return value, but I guess it should be 1 (meaning) one byte when dealing with sending multipart/form-data, am I right?

So should I just use strlen() for the strings like boundary string, Content-Disposition, Content-Type and count «\r\n» as a single char and use filesize() to get the filesize of the file in bytes and then sum all the values to get the final value for the Content-Length header? Or should I just count «\r\n» as 2 char as PHP does?

Thanks for the attention!

php curl data raw. Смотреть фото php curl data raw. Смотреть картинку php curl data raw. Картинка про php curl data raw. Фото php curl data raw

1 Answer 1

Count «\r\n» as two bytes. The CRLF pair is two bytes long. The entire body including

all should be accounted for in the content-length.

As for the content-length. There is no content-length defined for each file part in a multi-part request as of HTTP 1.1.

However, gross Content-length header should be defined in the beginning of the request. You may wish to read the RFC 7230 Section 3.3.2. It has been written by one of the principle web and HTTP standards author Roy Thomas Fielding.

But there are numerous other ways in which a file can be sent as a request, it doesn’t necessarily need to be bounded in a boundary based multipart request. The other mechanism in POST method itself is raw content type request. The content is transmitted just after the headers end in the request, followed by an empty line (read «\r\n»).

Other mechanisms include PUT requests which can also work in the similar fashion. Both of these have differences that separate them wide apart, technically, although I don’t think going into that detail has any concern with the question, so I’ll just skip it.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *