How to get Header & data-raw Of Curl Response from PHP - php

Using third party service; they are providing webhook services; I have given my url to send the response on my page:
Example: https://abcd.com/postback.php
Getting curl response on postback.php; to read the POST data; I am using following code:
$postdata = file_get_contents("php://input");
but not able to read the header & data content.
Sample Response that they are sending:
curl -L -X POST 'https://abcd.com/postback.php' -H 'x-changenow-signature: xcgh123POPI6727kslsldjsd' -H 'Content-Type: application/json' --data-raw '{"status":"finished","payinHash":"31mrEanXyv","payoutHash":"485d8dz","payinAddress":"3Nups0bF","payoutAddress":"1K1LcQSPZVj2dM","fromCurrency":"sol","toCurrency":"btc","amountSend":0.06676453,"amountReceive":0.00010273,"refundAddress":"Bk1qVzc3GULcJQ0NoW4DsepRJHevzAD8ynznEQ1aNzGq","id":"f7bed8a9e4a350","updatedAt":"2022-06-23T11:22:38.728Z","createdAt":"2022-06-23T11:09:56.403Z","expectedReceiveAmount":0.0001024,"isPartner":false}
Please suggest how to get header and data part.

The headers are in $_SERVER. Use $_SERVER['HTTP_X_CHANGENOW_SIGNATURE'] to get the x-changenow-signature header.
The data content is in $postdata. Use
$data = json_decode($postdata);
to convert it to an object. Then you can use $data->status, $data->payinHash, etc. to get the fields of the post data.

Related

How to send PHP input stream data using curl?

POST
On my local machine, I want to use cURL as an HTTP client like so:
curl -X POST -F 'email=derp#example.com' -F 'password=blink182' http://example.com
The above curl statement uses the HTTP POST method which can be retrieved in PHP like so:
echo $_POST['email']; // derp#example.com
echo $_POST['password']; // blink182
php://input
However, what I really wanted is the data from the PHP input stream php:://input and not from the POST method $_POST.
The PHP input stream can be retrieved in PHP like so:
$input = json_decode( file_get_contents( 'php://input' ) );
echo $input->email; // derp#example.com
echo $input->password; // blink182
Which brings me to my question, how to send PHP input stream data using curl?
From the PHP website:
php://input is a read-only stream that allows you to read raw data from the request body. php://input is not available with enctype="multipart/form-data".
So if you specify the Content-Type as application/json using -H "Content-Type: application/json" you ought to get it in the input stream
complete example:
curl -X POST https://reqbin.com/echo/post/json
-H 'Content-Type: application/json'
-d '{"email":"derp#example.com","password":"blink182"}'

How to Post data from external HTML form to Eloqua Form using cURL

I want to post data from an external HTML form to a simple form created in Eloqua, using cURL method. I tried to follow this documentation but I am not able to post data to Eloqua.
When I try from command line (Windows) -
curl --user "usercreds" --header "Content-Type: application/json" --request POST --data "{"testfirstname":"abc","testlastname":"def","singleCheckbox":1}" https://secure.p0{POD Number}.eloqua.com/api/REST/1.0/data/form/{formid}
I get below error:
[{"type":"ObjectValidationError","property":"fieldValues","requirement":{"type":"NoDuplicatesRequirement"},"value":""}]
When I try from PHP, as mentioned here https://www.eehelp.com/question/using-curl-to-repost-to-eloqua-data/
or https://github.com/fredsakr/eloqua-php-request the curl returns HTTP code 0.
This is a simple form created in Eloqua without any validations.
I do not know what I am doing wrong here.

How put the data back after getting it from the stream?

I am getting post data like this
$data = file_get_contents('php://input');
I want the data put back to the stream, so I modified the method following instaed of above method
$stream = fopen('php://temp', 'w+');
stream_copy_to_stream(fopen('php://input', 'r'), $stream);
rewind($stream);
But it is not working. Can any one tell where I did wrong?
This case is need in wordpress plugin developement. If one plugin reads the post data, other plugin not able to get that data.
Thanks
As stated in the documentation, php://input can only be read once and doesn't support seek operations.
The only way to 'read' the input stream multiple times is through passing its copy in your program.
PHP input stream contains request body and your sample will work only if request will contain body.
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d 'sample body' "YOUR_ADDRESS"
For check add to code:
echo fgets($stream);
php://input is not available with enctype="multipart/form-data".
http://php.net/manual/en/wrappers.php.php

POST json returns 400 with curl but functional test working

I am trying to build the POST of an API using Symfony2 and FOSRestBundle. The following functional test returns OK.
public function testPostArticleAction(){
$this->client->request(
'POST',
'/api/v1/articles.json',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{"articleContent":"The content of the content"}'
);
$response = $this->client->getResponse();
$this->assertJsonResponse($response,201, false);
}
But when I try to send a request via Curl with the same request body, it gives me a 400 invalid json message:
{"code":400,"message":"Invalid json message received"}
Here are the curl commands I have tried:
curl -X POST -d '{"articleContent":"title1"}'
http://localhost:8000/api/v1/articles --header
"Content-type:application/json"
curl -X POST -d '{"articleContent":"title1"}'
http://localhost:8000/api/v1/articles.json
Please to note that the GET returns to me a json like:
{"id":68,"article_content":"contents contents"}
But my field is articleContent in my doctrine mapping file. What am I missing?
Your help is much appreciated.
Try updating your header option to:
"Content-Type: application/json"
Additionally - does this request require any type of authentication? You'd need to pass in an auth cookie if so.

HTTP_Request2 POST parameters ignored?

A query with curl, like this, works fine:
curl -XPOST -H "Content-Type: application/json" -d '{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}' http://localhost:8080/services/rest/index/z56508/search/field/search
I my case I get 11 hits there. However when I try to translate this to HTTP_Request2 the call returns all hits in database instead.
I have looked at Http_Request2 POST request not working to write the code here:
require_once 'HTTP/Request2.php';
$url = "http://localhost:8080/services/rest/index/z56508/search/field/search";
$data = array("query"=>"porges","start"=>"0","rows"=>"10","lang"=>"ENGLISH");
$r = new HTTP_Request2($url);
$r->setHeader('Content-Type', 'application/json;charset=UTF-8');
$r->setMethod(HTTP_Request2::METHOD_POST)
->addPostParameter($data);
$response = $r->send();
$body = $response->getBody();
echo $body;
What am I doing wrong? It seems like "query" => "porges" is ignored, but why?
Read the Friendly Manual: http://pear.php.net/manual/en/package.http.http-request2.request.php#package.http.http-request2.request.body
addPostParameter() should be used when you want to generate a POST request body according to application/x-www-form-urlencoded or multipart/form-data Content-Type rules (hint: this is not JSON you are trying to send). If you have a pre-built request body, use setBody():
$request->setBody('{"query":"porges","start":0,"rows":10,"lang":"ENGLISH"}');

Categories