In PS I made this:
$body=#{
'code'='$code'
'access_token'=$token
}
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $body
Works perfect. How can I do the same in PHP Curl ?
Ive tryed something like this:
$data = array("code" => $code, "access_token" => $token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"code=" . $code,
"access_token=" . $token
));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('ContentType:text/plain'));
$result=curl_exec ($ch);
print_r($result);
But I keep getting error like : Parameter token is missing.
Related
I am getting the following response from the Facebook Ad Library API
{"error":{"message":"(#803) Some of the aliases you requested do not exist: \u003CAPI_VERSION>","type":"OAuthException","code":803,"fbtrace_id":"AXZ00I_mwgTvJwqIImb1SBw"}}
Below is the CURL I am trying to execute;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/<API_VERSION>/ads_archive');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "search_terms='california'&ad_type=POLITICAL_AND_ISSUE_ADS&ad_reached_countries=['US']&access_token=330479468051422|t_w8UDcd2QYqQ_hfxfs-LsKSxIA");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
?>
Is there anything you can see that I have done wrong, much apprecaited thankyou.
To upload objects to google cloud storage buckets, I need an authentication token. I have chosen JSON API method to upload objects to buckets.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/upload/storage/v1/b/Mybucket/o?uploadType=media");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "yourdata");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
$headers[] = "Authorization:Bearer <TOKEN>";
$headers[] = "x-goog-project-id: xxxxxxxxxxx";
$headers[] = "x-goog-user-project: xxxxxxxxxxx";
$headers[] = "Content-Length: 8";
$headers[] = "Content-Type: image/jpeg";
$headers[] = "x-goog-acl: public-read-write";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
Is there any way to upload object without these type of
authentication or any other authentication method?
How to generate this token (bearer) programmatically using api/curl/php?
function getOAuthToken(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Metadata-Flavor: Google";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
// echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$result = json_decode($result,true);
return $result['access_token'];
}
$result['access_token'] is the bearer token.
here is my script of php to call remote restful api, the username password are hard coded in source code,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost/m1/m1apiserver/public/api/login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"email=elson%40bla.com&password=bla");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec ($ch);
curl_close ($ch);
$j = json_decode($json);
// further processing ....
if ($j->token) {
//echo $j->token;
$output = file_get_contents("http://localhost/m1/m1apiserver/public/api/product?token=".$j->token);
echo $output;
}
but are there any techniques to secure the username password?
I want use shuttlecloud for import contacts from services. When i try make request like described here, i receive response
{"error": {"msg": "Missing parameter: timestamp", "id": "400"}}
Here me code:
my $ua = LWP::UserAgent->new;
my $url = 'https://demo.api.shuttlecloud.com/beta/load';
my $appid = 'some_app_id';
my $ts = int(time);
my $secret = 'some_secret';
my $sig = sha256_hex("{".$appid.":".$secret.":".$ts."}");
my $data = '{
"userid": {
"email": "admin#gypost.com"
}
}';
my $json = decode_json($data);
$url = $url."?appid=".$appid."&ts=".$ts."&sig=".$sig.;
my $req = HTTP::Request->new(POST => $url);
$req->content($json);
my $resp = $ua->request($req);
my $res = $resp->decoded_content;
print "Resp $res\n";
What i do wrong?
Instead of adding the following Header information it is showing HTML tag in PHP Mail.
$headers = "From: ".$name."<".$email.">\r\n";
$headers .= "Reply To: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
Please help me.
Try this,
$subject = 'Mail Subject';
$message = 'Test';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= 'From: ".$name."<".$email.">' . "\r\n";
$headers .= 'Reply-To: ".$email."' . "\r\n";
mail($to, $subject, $message, $headers);
Example from my mail()
$header = "From: Backapp.ru <noreply#backapp.ru>\r\nContent-type: text/html; charset=utf-8";
The basic usage of mail() function with headers is:
<?php
$to = "you#local.com";
$subject = "My email test.";
$message = "Hello";
$headers = "From: myplace#local.com\r\n";
$headers .= "Reply-To: myplace2#local.com\r\n";
$headers .= "Return-Path: myplace#local.com\r\n";
$headers .= "CC: sombodyelse#local.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
You don't send us your output so we don't know wheres the problem.