I am using https://apptweak.io api,
they didn't mention PHP example in their code example...
please tell me, how to understand and write Curl API example into PHP curl
Here is simple "appteak.com" curl example
curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"
Here is other example
$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl
How to write these curl in PHP?
i did this in php, after google, but its not work for me.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => "my_api_key",
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp ); //return false
I find correct answer
$token = "my_api_key";
$url = "http://URL.com";
$options = array('http' => array(
'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);
$result = file_get_contents($url, 0, $context);
Related
I am trying to send a php post request to my rocket chat server, I was able to do that using curl from command line by using this command from rocket chat api:
curl -H "X-Auth-Token: xxxxx" -H
"X-User-Id: yyyyy" -H "Content-type:application/json"
http://example:3000/api/v1/chat.postMessage -d '{ "channel":
"#general", "text": "Halo from Germany" }'
But using php I never succeed to do that (by using curl or without)
The following php code return false:
<?php
$url = 'http://example:3000/api/v1/chat.postMessage';
$data = json_encode(array('channel' => '#general', 'text' => 'Halo from Germany'));
$options = array( 'http' => array( 'header' => 'Content-type: application/json', 'X-Auth-Token' => 'xxxxx', 'X-User-Id' => 'yyyyyy', 'method' => 'POST', 'content' => http_build_query($data) ) );
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>
Thank you for your help
php has a (parital) wrapper for libcurl, the same library that the curl cli program use under the hood to do requests, you can just use libcurl from php.
<?php
$ch = curl_init ();
curl_setopt_array ( $ch, array (
CURLOPT_HTTPHEADER => array (
"X-Auth-Token: xxxxx",
"X-User-Id: yyyyy",
"Content-type:application/json"
),
CURLOPT_URL => 'http://example:3000/api/v1/chat.postMessage',
CURLOPT_POSTFIELDS => json_encode ( array (
"channel" => "#general",
"text" => "Halo from Germany"
) )
) );
curl_exec ( $ch );
curl_close($ch);
So I'm trying to cancel notifications using the REST API of oneSignal. I have successfully sent them and scheduled them using the oneSignal REST API but canceling is proving to be difficult. Mostly it is because in the documentation the cURL is:
curl --include \
--request DELETE \
--header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \
https://onesignal.com/api/v1/notifications/{notificationId}?app_id={appId}
This is my PHP code so far:
$ch = curl_init();
$httpHeader = array(
'Authorization: Basic MY_REST_API_KEY'
);
$url = "https://onesignal.com/api/v1/notifications/" . NOTIFICATION_ID . "?app_id=" . APP_ID;
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
I'm not too sure how to convert the shell to php and right now it is not deleting the notifications. the $response isn't returning anything meaning it is not working. Any help would be awesome.
Thanks
I had to add one more option to my options array so basically this:
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_SSL_VERIFYPEER => FALSE
);
That allowed the request to go through!
I'm trying to figure out how to do this CURL command:
curl https://uploads.stripe.com/v1/files \
-u sk_test_OCHANGEDPcDr0: \
-F purpose=identity_document \
-F file="#/path/to/a/file.jpg"
In PHP, I'm using this PHP script:
https://github.com/php-mod/curl
And tried this:
$curl->setBasicAuthentication('-u', 'sk_live_CHANGED');
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => $targetFile,
));
But I think the syntax is completely wrong, I'm really new to curl and don't understand what all the different -u and -F commands PHP equivalents are.
The -u username:password option translates to:
$curl->setBasicAuthentication('username', 'password');
To upload a file, you need to put # before the filename, just like in the curl command.
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => '#' . $targetFile,
));
You'll probably want to try something like this:
$headers = array(
'Authorization: Bearer ' . $your_api_key,
'Content-Type: multipart/form-data'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://uploads.stripe.com/v1/files',
CURLOPT_POST => true,
CURLOPT_INFILESIZE => '10000',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array(
'purpose' => 'dispute_evidence',
'file' => '#' . realpath($path_to_file)
)
));
$resp = curl_exec($curl);
curl_close($curl);
EDIT: Alternatively, you can upload files directly from the browser like this.
If you have a functional curl command invocation, you can get what you are after by adding the --libcurl filename.c option.
This will generate a C program to do what your command line did. Though you are after PHP and not C, this is still useful because they both use the same names for various options.
When I run the following cURL call natively I get an expected result.
curl -X POST -H "Accept:application/json" -H "Content-Type: application/x-www-form-urlencoded" "https://XXXX" -d 'grant_type=authorization_code&code=YYYY
When I run the cURL call via PHP I get a different response.
$curl = curl_init('https://XXXX');
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-type: application/x-www-form-urlencoded'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => 'YYYY'
),
));
$response = curl_exec($curl);
curl_close($curl);
Is there any functional difference between the native call and the PHP approach? Thanks.
The problem is that PHP's cURL doesn't trust the server's HTTPS certificate. Differently command line curl by default does.
The quick fix is to configure cURL to always trust the server:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
The proper fix is described here
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
I'm running into an issue with formatting using the curl_setopt functions in PHP. I'm basically trying to re-create the cURL request below, but my code returns a bad request from the server. I'm pretty sure it has to do with poor formatting, but I can't figure out where I went wrong.
//This code returns the data back successfully
curl -H "Content-Type: application/json" -d '{"bio_ids": ["1234567"]}' http://localhost:9292/program
<?php //This code returns a bad request from the server
$bio = array('bio_ids'=>'1234567');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:9292/program',
CURLOPT_POST => 1, // -d
CURLOPT_POSTFIELDS => $bio,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'), // -H
));
$resp = curl_exec($curl);
curl_close($curl);
?>
There are two issues:
You need to make sure that the structure of $bio matches what you are expected to pass, so the $bio declaration needs to be:
$bio = array('bio_ids' => array('1234567'));
Secondly you need to json_encode this data structure before sending it to the server:
CURLOPT_POSTFIELDS => json_encode($bio),
<?php //This code returns a bad request from the server
$bio = array('bio_ids'=>'1234567');
$bio = json_encode($bio);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:9292/program',
CURLOPT_POST => 1, // -d
CURLOPT_POSTFIELDS => $bio,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'), // -H
));
$resp = curl_exec($curl);
curl_close($curl);
?>