I'm trying to integrate a product and the example curl request is as follows.
curl https://a.klaviyo.com/api/v1/lists -G \
-d api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed
What I don't understand is what does -G and -d signify and how do I translate this request into PHP code?
-G stand for a get request and -d is the data passed to the get
in php you do
file_get_contents('https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');
-G, --get
When used, this option will make
all data specified with -d, --data, --data-binary or --data-urlencode
to be used in an HTTP GET request instead of the POST
request that otherwise would be used. The data will be appended to the URL with a '?' separator.
If used in combination with -I, --head, the POST data will instead be appended to the URL with a HEAD request.
If this option is used several times, only the first one is used. This is because undoing a GET doesn't make sense, but you should then instead enforce the alternative method you prefer.
$ch = curl_init();
$data = array('api_key'=>"pk_e29b4ec921f6aed9a70eb1e6993bb5caed");
curl_setopt($ch, CURLOPT_URL, "https://a.klaviyo.com/api/v1/lists");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
Just try this code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
// Decode the response into a PHP associative array
$response = json_decode($response, true);
echo "<pre>";print_r($response);
Related
The documentation says like this:
curl —X POST -c cookies.txt —d "login=demo&password=demo42" https://www.myadcash.com/console/login_proxy.php
Output will be:
{"token":"6333531373034343433623663646836383165693937383167373264323334663"}
My current code
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
curl_setopt($ch,CURLOPT_URL,"https://www.myadcash.com/console/login_proxy.php");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
No JSON response is showing. Although the text file is saving. Please help me to find right direction. Also if there is any error in the code, please let me know.
-d "login=demo&password=demo42" means data field, thats why pass as post fields not headers.
Therefore these two lines in your curl
curl_setopt($ch, CURLOPT_POST, 1); //Optional
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=demo&password=demo42");
and you must get output.
On the basis of documentation, credentials must be post fields not header that's why no need to put login and password on header.
You don't need
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
I need to post those values to php. In the documentation they give like:
curl -X POST 'https://api.telapi.com/v2/Accounts/{AccountSid}/Calls/{CallSid}/Recordings.json' -u '{AccountSid}:{AuthToken}' -d 'Record=true'
Here is the full documentation; http://docs.zang.io/docs/voice-effects
I have to set the voice pich, octaves, rate and such as defined in that page.
How do I do that ?
You probably need something like:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://site.tld" );
curl_setopt($ch, CURLOPT_USERPWD, "AccountSid:AuthToken"); # -u option
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Record=true"); # -d option
$result=curl_exec($ch);
You don't need to set curl_setopt($ch, CURLOPT_POST, 1);, POST is used by default when CURLOPT_POSTFIELDS is set.
I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F
First off, iam quite new with both PHP and cURL
From the command:
curl -i 'http://xxx:xxx/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}'
I want to produce the results of this command using POST/GET and PHP. I have an apache server and working url.
I just dont know how to make a working code that takes the code and produce the output to an empty page using PHP.
I have been searching examples and I know you got to make use of some of the following:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
I just dont know how to attach the proper flags from the cURL command to the PHP code, probably missing a few vital things aswell to wrap the code up.
Thanks in advance and sorry if I could not explain the my problem good enough.
** ***EDIT:* ****
So I made a code, that works for my needs, it produces a raw result from the cURL command on a PHP page.
Here is the code:
<?php
$request_body = '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}';
$ch = curl_init('http://xxx:xxx/v2.0/tokens');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
var_dump($response);
?>
Question now if I can manipulate the output to display in a better fashion, right now its just a big string, some form of JSON pretty print would be amazing.
Read more about curl exec (http://www.php.net/manual/en/function.curl-exec.php)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
print $data;
?>
The data is returned from the curl_exec command.
Whilst on a different subject, perhaps Uploading files to Zoho API with PHP Curl, a question I posted here recently which contains a Curl class example will help you.
In addition, I suggest you read the manual pages for the PHP curl methods e.g. the curl_exec page - the comments sections often include implementation suggestions.
I am trying to write PHP code for following API:
curl -u ********* -d email=tester#gmail.com \
--data-urlencode msg="First message from domain API" \
--data-urlencode url="http://www.domain.com/welcome" \
https://api.domain.com/1/send
PHP Code:
<?php
$url = "https://api.domain.com/1/send ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_USERPWD, "*********");
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=test#outlook.com&msg=hi%url=http://yahoo.com/"); // add POST fields
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
What am I doing wrong to process the above API?
Try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("email=test#outlook.com&msg=hi&url=http://yahoo.com/"));
There may be other errors (I can't easily test your code) but that should help. Notice that, in addition to adding the urlencode function, I also changed a % to a &
This question will probably help you, too.