How to connect to the new assembla key? - php

The new assembla api provides by REST-access a new authentification. i would like connect with PHP and curl, but I am not sure how I can include the api-x-key and api-x-secret as options:
The invoke with curl in terminal:
curl -H "X-Api-Key: XXX" -H "X-Api-Secret: XXX" https://api.assembla.com/v1/spaces/XXX/tickets.json
in PHP (my problem):
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_POSTFIELDS => ???maybe???
));
$response = curl_exec($ch);
print_r($response);
This is my first try, without the options from api-key/api-secret including.

Send those keys as headers. Try this:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
Hope this helps.

Assembla API in PHP:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
This code prints a blank page.
No response
help required please

Related

Pass Params in Curl Request

I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$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);

PHP cURL - authorization token missing

I am trying to cURL apptweak (ref - https://apptweak.io/api )
curl -H 'X-Apptweak-Key: your-api-key' https://api.apptweak.com/ios/applications/284882215.json
I have my key and can curl from the terminal. In PHP, I get "authorization token missing".
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/ios/applications/284882215.json&country=US&language=en',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
X-Apptweak-Key => 'MY-KEY-IS-HERE'
)
));
$resp = curl_exec($curl);
print $resp;
curl_close($curl);
Is X-Apptweak-Key => 'MY-KEY-IS-HERE' being a POST field the issue here?
What is wrong?
you can add X-Apptweak-Key between single quotes it's a key
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => 'MY-KEY-IS-HERE'
)
or you can try this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic MY-KEY-IS-HERE'));
or you can use:
curl_setopt($ch, CURLOPT_USERPWD, "X-Apptweak-Key:MY-KEY-IS-HERE");

How do I connect to the PredictIt API (type application/xml) using PHP/CURL?

This code is not returning anything:
<?php
$ch = curl_init();
$header = array('Contect-Type: application/xml', 'Accept: application/xml');
$options = array(
CURLOPT_URL => 'https://www.predictit.org/api/marketdata/ticker/RNOM16',
CURLOPT_HTTPHEADER => $header,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);
curl_close($ch);
echo '<p>Data</p>';
print_r($data);
?>
Does anyone know what's wrong?
Thank you.

Image upload in PHP error - type required

I'm trying for days to upload an image through PHP and OAuth2 to App.net.
Below is the PHP I'm using - it results in this error:
"error_message":"Bad Request: 'type': Required."
<?php
function sendPost()
{
$postData = array(
'type' => 'com.example.upload',
);
$ch = curl_init('https://alpha-api.app.net/stream/0/files');
$headers = array('Authorization: Bearer '.'0123456789',
'Content-Disposition: form-data; name="content"; filename="http://www.example.com/pics/test.jpg";type=image/jpeg',
'Content-Type: image/jpeg',
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
));
$response = curl_exec($ch);
}
sendPost();
?>
This is their cURL example from the API documentation:
curl -k -H 'Authorization: BEARER ...' https://alpha-api.app.net/stream/0/files -X POST -F 'type=com.example.test' -F "content=#filename.png;type=image/png" -F "derived_key1=#derived_file1.png;type=image/png" -F "derived_key2=#derived_file2.png;type=image/png;filename=overridden.png"
What type is required and what do I need to change to make it work?
Any feedback is really appreciated. Thank you.
You need to do these following changes
$headers = array(
'Authorization: Bearer '.'0123456789'
);
$postData = array('type' => 'com.example.upload', 'content' => '#/roor/test.png');
Also use this as you are using https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);

Send a data request with headers

I'm using Twitter API v1.1. I've created a valid authorization header (using 0auth).
Now I want to actually send a request to Twitter for the data I want but I'm fairly new to PHP and certainly haven't got a damn clue about cURL.
So far I've got:
$authHeader = 'Authorization: 0Auth ....... Expect:'
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
Then I found the following code in twitterAPIexchange which I can't get working for me:
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
Can anyone help me with the header formation to make this request?
You must pass the headers as an array, e.g.
curl_setopt($feed, CURLOPT_HTTPHEADER, array('HeaderName1: HeaderValue1', 'HeaderName2: HeaderValue2'));
Or, in your case,
$authHeader = array('Authorization: OAuth oauth_consumer_key...');
$baseURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$screenName.'&count='.$tweetCount;
$options = array(
CURLOPT_HTTPHEADER => $authHeader,
CURLOPT_HEADER => false,
CURLOPT_URL => $baseURL,
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

Categories