How to get survey ID by name in SurveyMonkey - php

How to get survey ID by name in SurveyMonkey?
This is what is found, but how to convert this into PHP?
curl -i -X POST -H "Authorization:bearer YOUR_ACCESS_TOKEN" -H "Content-Type": "application/json" https://api.surveymonkey.net/v3/surveys -d '{"title":"New Survey"}'
Here is my basic api call without passing the survey title parameter:
<?php
$requestHeaders = array(
'Content-Type: application/json',
'Authorization: Bearer 12345',
);
$url = 'https://api.surveymonkey.net/v3/surveys/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_contactlist);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
?>
Thanks.
Solution:
Add
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

i found the solution: add the following lines:
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

Related

how to convert curl command into php script

I have a situation where i want to use curl functionality to get data by api call.
the curl command i'm interested to convert look like this:
"curl -X POST \\ \n'http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806' \\ \n -H 'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU= \\ \n -H 'cache-control: no-cache' \\ \""
I have tried this much
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array('merchantKey'=>40747,'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
please help me to correct above code!
thank in advance
You are missing a semicolon in your code and you don't need to declare the array as a variable.
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806));
PHP Code
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test.payumoney.com/payment/op/getPaymentResponse');
curl_setopt($ch, CURLOPT_POST, true);
// Add authorization header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU=',
));
// Remove $ from array()
curl_setopt($ch, CURLOPT_POSTFIELDS, array('merchantKey'=>40747, 'merchantTransactionIds'=>396132-58876806))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// For No-Cache
curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
$result = curl_exec($ch);
?>
You should do it like this, here i am assuming all your parameters are correct. You were missing some required parameter which were sending in curl command but not in PHP curl that is CURLOPT_HTTPHEADER and CURLOPT_HEADER and you should use http_build_query while sending array in CURLOPT_POSTFIELDS and You should use CURLOPT_FOLLOWLOCATION because your current is URL is redirecting from HTTP to HTTPS
<?php
ini_set('display_errors', 1);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://test.payumoney.com/payment/op/getPaymentResponse?merchantKey=40747T&merchantTransactionIds=396132-58876806");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('merchantKey'=>"40747",'merchantTransactionIds'=>"396132-58876806")));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('cache-control: no-cache',
'authorization: KpNTiy57L6OFjS2D3TqPod8+6nfGmRVwVMi5t9jR4NU='));
curl_exec($ch);

What is the LIBCURL equivalent to -d?

In LIBCURL for PHP what is the equivalent to a basic curl -d?
This is my basic CURL: I need to format it for LIBCURL in PHP:
curl -u username:password -H "Content-Type:application/json" -X POST -d '[{"phoneNumber":"12135551100","message":"Hello World!"}]' "https://api.example.com/v2/texts"
I have tried using CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATAbut I can't seem to get my request to work.
The option required is CURLOPT_POSTFIELDS. The specification is the same as the libcurl reference.
There are some PHP examples in the curl_setopt reference. The simplest way being the following example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2");
My endpoint needed data in the form of an array so this worked.
function doPost($url, $user, $password, $params = array()) {
$authentication = 'Authorization: Basic '.base64_encode("$user:$password");
$http = curl_init($url);
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http, CURLOPT_URL, $url);
curl_setopt($http, CURLOPT_POST, true);
curl_setopt($http, CURLOPT_POSTFIELDS, $params);
curl_setopt($http, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json', $authentication));
return curl_exec($http);
}
My array formatted like this:
$params = '[{"phoneNumber":"' . $ToNumber . '","message":"' . $msg . '"}]';

Converting command cURL to HTTP request

curl -X POST https://example.com/sandbox -u \
'username:password' -d 'vendor=123456' -d 'list_id=1000001' \
-H 'Accept: application/json
How would I structure a HTTP request with a command cURL like this, with a username/password?
You can use curl in PHP. I've created a example code for you:
$username='username';
$password='password';
$URL='https://example.com/sandbox';
$data=array('vendor'=>123456, 'list_id'=>1000001);
$payload = json_encode( $data );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$result=curl_exec($ch);
curl_close ($ch);
I hope that helps :D

Create a cURL request using PATH API

Guys. I am new to CURL so i have no experience on implementing CURL request. In this case, i want to post some data using CURL. Here is the CURL :
curl -H 'Authorization: Bearer <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \
-d '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}' \
https://partner.path.com/1/moment/photo
is there any of you guys know about implementing CURL request using the above data? Thank you very much.
finally, i found the answer. Here is how i create a request to the Path API
$url = 'https://partner.path.com/1/moment/photo';
$authorization = "Authorization: Bearer 8edf232243d58e4940d931490e882123432434f";
$headers = array($authorization,'Content-Type: application/json');
$json_data = '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );

translating a command line curl into php

i am not very curl savvy was wondering if anyone could help me turn the following into php:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}' https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret
if you know of a good curl tutorial would also be great help.
something like this
$ch = curl_init();
$json = '{"tester":{"email":"justin#prefinery.com","status":"applied","profile":{"first_name": "Justin", "last_name": "Britten"},"responses":{"response":[{"question_id":"23874", "answer":"a text response"},{"question_id":"23871", "answer":"1"},{"question_id":"23872", "answer":"0,2"},{"question_id":"23873", "answer":"9"}]}}}';
$url = 'https://account.prefinery.com/api/v2/betas/1/testers.json?api_key=secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);

Categories