I have got the following cURL command that is running expectedly:
curl -X POST
-H "Content-Type: application/json"
-d '{"duplicateEntitiesIds": [21,31,41]}'
{URL}:3003/v1/entities/5/merge
And I am trying to replicate that with Guzzle, which however fails, returning a 400 status code:
$request = $httpClient->post('{URL}:3003/v1/entities/'.$mainEntityId.'/merge',
['json' =>
['duplicateEntitiesIds' => $duplEntitiesIdsToArray]
]
);
$response = $request->send();
I have tried to change my post body , but it keeps failing. Any ideas would be appreciated.
NOTE
The data should be sent in the following format:
{"duplicateEntitiesId": [2,3,4]}
Related
I am trying to get the response of a Http post with curl in Jenkins, I have the following script:
curl -X POST -k -H "Accept: application/json" -H "Content-Type: application/json" --data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" http://mypage/Data/file.php
As you can see I am sending to file.php a json file, and then I am calling some functions and am returning a specific result.
With that script, I am getting the result I want, but I want to evaluate that result, let's say for example the result was "OK", then I want to assign the result to a variable, and then say if $result=="OK" then do this else do that. How can I do that, I have tried something like this:
if $response == "true" then exit 1 fi
But it does not seem to work out, does anyone know how it can be done?
They marked it as similar to this question
PHP cURL, extract an XML response
, but I don't see how, because I am not talking about php code, bash code, and I want to store the curl result in a variable....
Thanks in advance!!!
You can check the status code if you only need to check if the request was successful :
status=$(curl --write-out '%{http_code}' \
-s -o /dev/null \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "200" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
with :
--write-out '%{http_code}' : output the status code
-o /dev/null : doesn't to output the body
-s : doesn't display connection log
As you have specified Accept: application/json, you expect a response in JSON format, so you could use jq JSON parser to parse it :
If the response is :
{ "status": true }
then you can do the following :
status=$(curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php" | jq -r '.status')
if [ "$status" == "true" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
If the response is not in JSON format and the response is OK :
status=$(curl -s -H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "OK" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
I am trying to authenticate my self with uber rush api, and I keep getting an unsupported_grant_type error message. I am not sure what am I doing wrong here. Any help would be really appreciated. Below is the code I am using
Here is what the command line request looks like:
curl -F "client_secret=<CLIENT_SECRET>" \
-F "client_id=<CLIENT_ID>" \
-F "grant_type=client_credentials" \
-F "scope=delivery" \
https://login.uber.com/oauth/v2/token
Here is how I wrote it in PHP
$cl = curl_init("https://login.uber.com/oauth/v2/token");
curl_setopt($cl,CURLOPT_POST,["client_secret"=>"********"]);
curl_setopt($cl,CURLOPT_POST,["client_id"=>"**********"]);
curl_setopt($cl,CURLOPT_POST,["grant_type"=>"client_credentials"]);
curl_setopt($cl,CURLOPT_POST,["scope"=>"delivery"]);
$content = curl_exec($cl);
curl_close($cl);
var_dump($content);
This is not how to add POST data to cURL. There is PHP documentation to tell you what these options actually mean. Try this instead:
<?php
$postdata = [
"client_secret"=>"xxx",
"client_id"=>"xxx",
"grant_type"=>"client_credentials",
"scope"=>"delivery",
];
$cl = curl_init("https://login.uber.com/oauth/v2/token");
curl_setopt_array($cl, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => true
]);
$content = curl_exec($cl);
curl_close($cl);
var_dump($content);
This answer is also very useful if the above answer doesn't work: Curl Grant_Type not found FIXED.
I am working with a Wordpress site with CPanel and a MySQL database. I want to be able to read data from a MongoDB held on Parse.com. Eventually, I want to change Wordpress's login.php script to search through the MongoDB and create users if necessary.
I am having lots of trouble connecting to the database.
Here is my php script:
<?php
$url = 'http://mercury.example.com:2234/parse/login';
$data = array('username' => 'username', 'password' => 'password');
$appID = "X-Parse-Application-Id: parseAppID";
$restKey = "X-Parse-REST-API-Key: praseRESTapiKey";
$session = "X-Parse-Revocable-Session: 1";
$contentType = "Content-Type: application/json";
$context = array(
'http'=> array(
"method" => "GET",
"header" => $appID . $restKey . $session . $contentType,
"content" => http_build_query($data)));
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
The errors I am receiving are:
Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in C:\wamp\www\parseDB.php on line 26
Warning: file_get_contents(http://mercury.example.com:2234/parse/login): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in C:\wamp\www\parseDB.php on line 26
From my understanding, the 403 error means the web server is returning the "forbidden" status code.
I am testing my php script on localhost using WAMP. A colleague of mine tried to run a similar command on Bash and received a response. (I broke it out so it is easier to read).
curl -X GET
-H "X-Parse-Application-Id: parseAppID"
-H "X-Parse-REST-API-Key: parseRESTapiKey"
-H "X-Parse-Revocable-Session: 1"
-G --data-urlencode 'username=username' --data-urlencode 'password=password'
http://mercury.example.com:2234/parse/login
I have been stuck on this for 2 days so far, and I have no idea what is going on. I appreciate all the help I can get.
EDIT
Here is my final solution:
$url = 'http://mercury.example.com:3432/parse/login';
$data = array('username' => 'USERNAME', 'password' => 'PASSWORD!');
$context = array(
'http'=> array(
'method' => "GET",
'header' => "X-Parse-Application-Id: APPID\r\n" .
"X-Parse-REST-API-Key: RESTAPIKEY\r\n" .
"X-Parse-Revocable-Session: 1" .
"Content-Type: application/json\r\n",
'content' => http_build_query($data)
)
);
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
Look at the stream_content_create example at http://php.net/manual/en/function.stream-context-create.php which explains how to pass headers properly. At this time, you slam each header together without any line feeds which will make them look like concatenated string:
X-Parse-Application-Id: parseAppIDX-Parse-REST-API-Key: praseRESTapiKeyX-Parse-Revocable-Session: 1Content-Type: application/json
Hint - add \r\n after each header line.
Instead of file_get_contents you could also use curl methods.
I'm new to curl in PHP... and I was just wondering how to transform this curl command into PHP:
curl https://ancient-test.chargebee.com/api/v1/portal_sessions \
-u test_rdsfgfgfddsffds: \
-d customer[id]="EXAMPLE" \
-d redirect_url="https://yourdomain.com/users/3490343"
Right now I've got:
$post_data['customer']['id'] = "EXAMPLE";
$post_data['redirect_url'] = "http://" . SITE_URL . "/myaccount/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://ancient-test.chargebee.com/api/v1/portal_sessions");
curl_setopt($ch,CURLOPT_USERPWD,"test_rdsfgfgfddsffds:");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$output = curl_exec($ch);
curl_close($ch);
But I get the error message:
{"errors":[{"message":"There were errors while submitting"},{"param":"customer[id]","message":"cannot be blank"}]}
Thanks for your help!
Jan
https://github.com/CircleOfNice/CiRestClientBundle
This one has a beautiful api.
$client->post($url, $payload, $options);
Using curl here's probably the answer Posting with PHP and Curl, deep array
$post_data['customer[id]'] = "EXAMPLE";
Guzzle is an awesome HTTP client library wrapping curl in PHP that will make your life way easier :)
With guzzle v6 your php code would look like this :
$client = new GuzzleHttp\Client();
$res = $client->request('POST', 'https://ancient-test.chargebee.com/api/v1/portal_sessions', [
'auth' => ['test_rdsfgfgfddsffds', 'password'],
'json' => [customer => [id => 'EXAMPLE']]
]);
I'm using this curl command to send json data to a php webservice, but i'm not getting anything in the $_POST variable.
Here is the curl command
curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://192.168.2.127:8888/json.php -d '{"age":"234","password":"password"}'
and here is the php code.
<?php
header('Content-type: application/json');
var_dump($_POST);
$return_arr = array();
$age = $_POST['age'];
$ageInt = intval($_POST['age']);
$return_arr['age1'] = $age;
var_dump($_POST);
$return_arr['age2'] = $ageInt;
echo json_encode($return_arr);
?>
Thanks in advance
Once try this
curl -X POST -H "Content-Type: application/json" -d '{"age":"21343","password":"password"}' http://192.168.2.127:8888/json.php