I'm trying to get an PHP Curl script working [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 days ago.
Improve this question
I'm trying to get an PHP Curl script working but all I get is:
HTTP Status 400 - type Status report description The request sent by the client was syntactically incorrect.
My Code:
$ch = curl_init();
$clientId = "fd2ff35ee3xxxxxx";
$clientSecret = "780f7671b6e6bxxxxxxx";
$lockId = "24451";
$keyboardPwdType = "3";
$keyboardPwdName = "test";
$startDate = "1626945087000";
$endDate = "1636945087000";
$date = "1626945087000";
$fields = array('clientId'=>$clientId, 'clientSecret'=>$clientSecret, 'lockId'=>$lockId, 'keyboardPwdType'=>$keyboardPwdType, 'keyboardPwdName'=>$keyboardPwdName, 'startDate'=>$startDate, 'endDate'=>$endDate, 'date'=>$date);
$postvars = '';
foreach($fields as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$url = "https://euapi.ttlock.com/v3/keyboardPwd/get";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
print "curl response is:" . $response;
curl_close ($ch);
}
And this is the CURL code that works with SSH:
curl --location -g --request POST 'https://euapi.ttlock.com/v3/keyboardPwd/get' \
--data-urlencode 'clientId=fd2ff35ee3xxxxxx' \
--data-urlencode 'accessToken=780f7671b6e6bxxxxxxx' \
--data-urlencode 'lockId=24451' \
--data-urlencode 'keyboardPwdType=3' \
--data-urlencode 'keyboardPwdName=test' \
--data-urlencode 'startDate=1626945087000' \
--data-urlencode 'endDate=1636945087000' \
--data-urlencode 'date=1626945087000'
Anyone a solution i'm missing? Thanks!
I tryed different codes and options presented on Google and Stackoverflow, none working.

Related

How do I post to GraphQL from PHP

I have the following curl request to GraphQL. It works great, but in production shell_exex is not allowed. How do I re-write this curl post in valid PHP?
$curl_string = 'curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer "' . AIRTABLE_API_KEY;
$curl_second_string = ' -d \'{"query": "{fullCapaReview (id: \"' . $id . '\") {proposedRuleName submissionDate agencyContactName statusLawDept}}"}\' https://api.baseql.com/airtable/graphql/appXXXzzzzzzzzzz';
$curl_complete_string = "$curl_string $curl_second_string";
$result = shell_exec($curl_complete_string);
edit: I'm sorry, I put the wrong query. The query I had in mind was:
' -d \'{"query": "{dMsAgencies (agencyAcronym: \"' . $_agency . '\") {agencyAcronym fullCapaReview { id }}}"}\'
I make two similar calls. I will leave the original there because someone answered based on that.
This is what I have so far:
$curl = curl_init($url);
$query = 'query dMsAgencies($agencyAcronym: String) {agencyAcronym fullCapaReview { id }} ';
$variables = ["agencyAcronym" => $id ];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query, 'variables' => $variables]));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Authorization: Bearer ' . AIRTABLE_API_KEY]);
$response = curl_exec($curl);
curl_close($curl);
console_log("Response : " . $response);
This is the error message I am getting. I just want to see if I am in the ballpark with my syntax.
Response : {"errors":[{"message":"Cannot query field \"agencyAcronym\" on type \"Query\".","locations":[{"line":1,"column":44}],"stack":["GraphQLError: Cannot query field \"agencyAcronym\" on type \"Query\"."," at Object.Field (/var/app/current/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:46:31)"," at Object.enter (/var/app/current/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}],"stack":["GraphQLError: Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\"."," at Object.leave (/var/app/current/node_modules/graphql/validation/rules/NoUnusedVariablesRule.js:38:33)"," at Object.leave (/var/app/current/node_modules/graphql/language/visitor.js:344:29)"," at Object.leave (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:390:21)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]}]}
message":"Cannot query field \"agencyAcronym\" on type \"Query\"
{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}]
The queries are not the same, but assuming that you are aware, your PHP example also has a syntax issue.
query dMsAgencies($agencyAcronym: String) {
agencyAcronym
fullCapaReview {
id
}
}
If you compare this with the example in the docs (when using variables) you can see that you are currently not using the $agencyAcronym variable anywhere (and there probably isn't a query named agencyAcronym in your schema). Here is one example (using the query from your first snippet):
query dMsAgencies($agencyAcronym: String) {
fullCapaReview (id: $agencyAcronym) {
proposedRuleName
submissionDate
agencyContactName
statusLawDept
}
}

Curl to PHP with array, probably simple answer

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']]
]);

How to fetch information from rest in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to fetch information from rest in php?
I have an url and I wanna extract information from web service by php.
Example of calling GET request
//next example will recieve all messages for specific conversation
$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Reference : How to make REST calls in PHP
You can execute curl if is enable on your host.

How to specify --form parameter and PUT argument in php curl ?

I have to execute this curl command in php:
curl --digest -u YourApiKey:YourApiSecret "http://api.moodstocks.com/v2/ref/YourID" --form image_file=#"image.jpg" -X PUT
So far I have this:
function ms_addimage($file, $hash_id){
$postdata = array("image_file" => "#/".realpath($file));
$opts[CURLOPT_URL] = $this->API_BASE_URL . "ref/".$hash_id;
$opts[CURLOPT_VERBOSE] =1;
$opts[CURLOPT_POST] =true;
$opts[CURLOPT_POSTFIELDS] =$postdata;
$ch = curl_init();
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
echo "Response " . $raw_resp . "\n";
curl_close($ch);
}
The file path is correct but I am missing something.
How do I pass the --form parameter and the PUT argument?
So I was missing the following:
$opts[CURLOPT_CUSTOMREQUEST] = "PUT";
Now it is working.

How do I use the DailyMotion API? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone here help me with the integration of the DailyMotion API? I am using their SDK but to no avail, and forums are also not helping much.
I tried using the DailyMotion SDK. It worked very well but later on for some reason I was told not to use SDKs. So here is the PHP code to use the API using cURL.
define("DAILYMOTION_API_KEY", "xyzzz");
define("DAILYMOTION_API_SECRET_KEY", "abcc");
$testUser = "username";
$testPassword = "pwd";
$url = 'https://api.dailymotion.com/oauth/token';
$testVideoFile = "<file location>";
$vidName = "testing video";
$vidDesc = "this is a test";
/* GET ACCESS TOKEN */
try {
$data = "grant_type=password&client_id=" . DAILYMOTION_API_KEY . "&client_secret=" . DAILYMOTION_API_SECRET_KEY . "&username=abs&password=pwd&scope=read+write";
$curlInit = curl_init($url);
curl_setopt($curlInit, CURLOPT_POST, 1);
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlInit);
curl_close($curlInit);
$res = json_decode($output);
$accessToken = $res->access_token;
$getUploadUrl = "curl -d 'access_token=$accessToken' -G https://api.dailymotion.com/file/upload/";
$uploadUrl = json_decode(system($getUploadUrl));
$postFileCmd = "curl -F 'file=#$testVideoFile'" . ' "' . $uploadUrl->upload_url . '"';
$postFileResponse = json_decode(system($postFileCmd));
$postVideoCmd = "curl -d 'access_token=$accessToken&url=$postFileResponse->url' https://api.dailymotion.com/me/videos";
$postVideoResponse = json_decode(system($postVideoCmd));
$videoId = $postVideoResponse->id;
$publishCmd = "curl -F 'access_token=$accessToken' \
-F 'title=$vidName' \
-F 'published=true' \
-F 'description=this is a test' \
https://api.dailymotion.com/video/$videoId";
$publishres = system($publishCmd);
print_r($publishres);
echo "Video is posted & published Successfully";
} catch (Exception $e) {
print_r($e);
}

Categories