Passing X-Api-Key through file_get_contents - php

I am trying to get data from a JSON endpoint on my streaming server. I've read that i need to pass an API key through the X-API-Key header. But not sure how to do this.
$url = file_get_contents('XXXXX/history');
$data1 = json_decode($url,true);
var_dump($data1);
this code works for me when getting json data from files that dont require the api key. How ever this now returns NULL as the key isn't being submitted through.
Any ideas?

try this:
$url = 'XXXXX/history';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$yourApiKey
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

try with CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'XXXXX/history');
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $yourApiKey
));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Related

PHP Jira Service Desk REST API

I am struggling to create a request on Jira Service Desk in PHP.
My code is :
public function reportIssue(Request $request) {
//post
//authenticate to Jira ...
//create request ...
//response ....
//do something afterwards ... post ...
$jdata = json_encode($request);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request,
CURLOPT_USERPWD => SERVICE_USERNAME . ':' . SERVICE_PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
I am getting empty response body, still like something is wrong.
Pardon me if my mistake is obvious.
Had the same issue. Here is my working curl config:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, SERVICE_USERNAME . ':' . SERVICE_PASSWORD);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json;charset=UTF-8",
'X-ExperimentalApi: opt-in'
)
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($jdata));
$response = curl_exec($curl);

Trivia API not returning proper JSON in PHP

I am using this API
https://market.mashape.com/pareshchouhan/trivia
for making widget in my site but when i am calling this API through CURL it is returning following error:
Any solution for that?
Here my code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key" => "MY_KEY",
"Accept" => "application/json"
));
$data = curl_exec($ch);
curl_close($ch);
echo $data;
when i execute this PHP file, it is returning "Missing Mashape application key" in return data.
http://i.prntscr.com/5f98b1e8965f42eda29875543b052ec5.png
You are specifying you curl headers incorrectly. Look at the CURLOPT_HTTPHEADER option in the docs at http://php.net/manual/en/function.curl-setopt.php. Your code should look like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key: MY_KEY",
"Accept: application/json"
));
$data = curl_exec($ch);
curl_close($ch);

how to convert python httplib.HTTPSConnection to php

i want send a post HTTPS request with php ,similar this request in python:
API_HOST = "android.clients.google.com"
API_PAGE = "/market/api/ApiRequest"
parmas=params = urllib.urlencode({
"version" : 2,
"request" : request
})
headers = {"Content-type": "application/x-www-form-urlencoded"}
connection = httplib.HTTPSConnection( API_HOST )
connection.request( "POST", API_PAGE, params, headers )
resp = connection.getresponse().read()
connection->close()
I tried to this lib
https://github.com/dcramer/php-httplib
but this lib , not support HTTPS
my php code:
$query_data = array(
"version" => 2,
"request" => $request
);
$params = http_build_query($query_data);
$headers = array("content-type"=> "application/x-www-form-urlencoded");
$API_URL = $API_HOST.$API_PAGE;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$resp = curl_exec($ch);
python code works, but php code return Bad Request, Error 400
Can you try this:
<?php
// Data
$postfields = array(
'version' => 2,
'request' => $request
);
// Post It
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.clients.google.com/market/api/ApiRequest');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
// Debug
var_dump($result);

Creating Leads in SalesForce using REST API in PHP

I have been trying to create a lead from SalesForce's REST API for the past several days but I can't for the life of me get it working. I am able to get the access token no problem but from there on as far as creating a lead I am having absolutely no luck at all.
I keep seeing in all of the documentation this:
curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d #newaccount.json"
How would I do this in PHP's curl though? I have tried and tried but had no luck at all.
Here is how I have got the access token:
$ch = curl_init();
// set URL options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=".CONSUMER_KEY."&client_secret=".CONSUMER_SECRET."&username=".USERNAME."&password=".USERPASS.SECURITY_TOKEN);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab HTML
$data = curl_exec($ch);
$data = json_decode($data, true);
$code = $data['access_token'];
curl_close($ch);
I have tried doing something like this after this code, however I have had no luck.
$token_url = LOGIN_BASE_URL.'/services/oauth2/token';
$post_fields = array(
'code' => $code,
'grant_type' => 'authorization_code',
'client_id' => CONSUMER_KEY,
'client_secret' => CONSUMER_SECRET,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$token_request_body = curl_exec($ch)
I just need to figure out how to create leads in SalesForce, I have no idea where to go from here. Any help would be greatly appreciated as I cannot find decent documentation anywhere that helps me.
create account demo, should get you started:
function create_account($name, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Account/";
$content = json_encode(array("Name" => $name));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token",
"Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
echo "HTTP status $status creating account<br/><br/>";
curl_close($curl);
$response = json_decode($json_response, true);
$id = $response["id"];
echo "New record id $id<br/><br/>";
return $id;
}

curl POST error: The requested URL returned error: 413

I've looked around a bit and can't find a solution that solves my problem. I'm getting the following error: "The requested URL returned error: 413."
I'm posting some information via curl and PHP to a URL via HTTPS. I'm told that I have to pass the "Content-Length" along with my request since the destination is https and not http.
Here's the code I'm using:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$api = curl_exec($ch);
My strlen($data_string) command is returning a value of 5, which is much less than the actual length of the $data_string, which is much longer. I assume this is the problem unless someone thinks it might be being caused by something else.
Here's what I ended up with:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$api = curl_exec($ch);
No SSL or size errors and it seems to work fine.

Categories