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"}');
Related
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.
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"}'
I am attempting to use the polar php sdk to retrieve data from the polar accesslink api. I am able to get through the Oauth2 workflow but am stuck after receiving the access token. Following the documentation I am attempting to register a user using the following code:
$config = Configuration::getDefaultConfiguration()->setAccessToken($this->token);
$apiInstance = new UsersApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new \GuzzleHttp\Client(),
$config
);
$body = new Register(); // \Coachbox\Services\Polar\Models\Register |
try {
$result = $apiInstance->registerUser($body);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling UsersApi->registerUser: ', $e->getMessage(), PHP_EOL;
}
However the sdk throws the following error: [400] Client error: POST https://www.polaraccesslink.com/v3/users resulted in a 400 Bad Request
If instead use a curl command as follows (but through php):
curl -X POST https://www.polaraccesslink.com/v3/users \
-H 'Content-Type: application/xml' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer {access-token}'
Body parameter
{
"member-id": "User_id_999"
}
I get an empty response. I feel I must be missing an important step, but I do not know what it is. Any help is greatly appreciated.
EDIT:
Heard back from Polar Support, there is an error in their API documentation. The -H 'Content-type' should be 'application/json'. But, if I make the change I get a new error:
"Unrecognized token 'member': was expecting ('true', 'false' or 'null')
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 8]"
EDIT 2: I was able to get the code working through a cURL request (I didn't have my body formatted as json properly). However, I am still unable to get the sdk working.
Did a bit of debugging on the same issue and it turns out the SDK example misses the vital step of setting the member_id
It should be:
$body = new Register();
$body->setMemberId($your_member_id);
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.
I'm using the PEAR library Http_Request2. I've been unsuccessfully searching for documentation on how to make a PUT request and pass parameters to a webservice. Can anyone provide some help?
For a POST request, it's easy:
$request = new HTTP_Request2 ( "http://my.url.com");
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->addPostParameter('data', "blah"); //easy to add post params...
$response = $request->send();
However, I can't figure how to send data when changing the method to PUT:
$request = new HTTP_Request2 ( "http://my.url.com");
$request->setMethod(HTTP_Request2::METHOD_PUT);
// ????? missing secret sauce to add data to put request....
$response = $request->send();
Anyone lend a hand?
You need to use setBody() to set your PUT data. Don't forget the header, e.g.
$request->setHeader('Content-type: application/json');
$request->setBody('{"foo":"bar"}');
$response = $request->send();