I'm trying to get some data from an api that is protected by http authentication.
I'm want the response to be a json, but I'm getting a string innstead.
What can I do to fix this?
This is my code:
//init curl
$ch = curl_init();
$process = curl_init($url."?".$queryString);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json', ''));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, '');
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
print_r($return);
Related
while creating share link with the help of ocs share api i am able to create the link but it is not setting the permission that i am trying to give it.
$ch = curl_init("https://servername/ocs/v2.php/apps/files_sharing/api/v1/shares");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', "OCS-APIRequest:true"));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, getenv('NEXTCLOUDUSERNAME') . ":" . getenv('NEXTCLOUDPASSWORD'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
function curlPost($url, $headers, $username, $password, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
return curl_exec($ch);
curl_close($ch);
}
$url = 'https://SERVERNAME/ocs/v2.php/apps/files_sharing/api/v1/shares';
$headers = Array('OCS-APIRequest: true');
$username = 'NEXTCLOUDUSERNAME';
$password = 'NEXTCLOUDPASSWORD';
$data = 'path=PATH/FILE.xxx&shareType=3&permissions=1&format=json';
echo curlPost($url, $headers, $username, $password, $data);
$process = curl_init();
$temporary_cookie_file = 'temporaryfiles/'.strtolower($user_general_information['Username']).'_cookies.txt';
$temporary_cookie_file_handler = fopen($temporary_cookie_file, 'w');
fclose($temporary_cookie_file_handler);
$user_data = array('TCNK'=>'authenticationEntryComponent', 'submitEvent'=>'1', 'guestLoginEvent'=>'', 'enterClicked'=>'true', 'bscf'=>'', 'bscv'=>'', 'targetEntid'=>'', 'ajaxSupported'=>'yes', 'screenName'=>$user_general_information['Username'], 'kclq'=>$user_general_information['Password']);
curl_setopt($process, CURLOPT_URL, 'https://www.edline.net/post/InterstitialLogin.page');
curl_setopt($process, CURLOPT_USERAGENT, 'Chrome/12.0.742.122');
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($user_data));
curl_setopt($process, CURLOPT_TIMEOUT, 0);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($process, CURLOPT_COOKIEJAR, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_COOKIEFILE, realpath($temporary_cookie_file));
curl_setopt($process, CURLOPT_HEADER, false);
$results_1 = curl_exec($process);
var_dump($results_1);
die();
I am trying to use POST form data to log into a site and then store the created cookies in a temporary file. I've looked at other discussions that talk about doing this and did as them but it doesn't work. When I try, it simply refreshes the page with no changes. I've been working on this for hours...Any ideas of what I am doing wrong?
I am using postmates API for my book store.
I am using their test credentials, but I don't know how to use it. Kindly guide me how can I make a basic api call.
I am using PHP. I want to send post data using cURL
Thank you
<?php
$url = "https://api.postmates.com/v1/customers/my-customer-key/delivery_quotes";
$uname = "my-api-key";
$pwd = null;
$data = array(
'dropoff_address' => '20 McAllister St, San Francisco, CA 94102',
'pickup_address' => '101 Market St, San Francisco, CA 94105',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
?>
From the documentation:
The Postmates API requires authentication by HTTP Basic Auth headers. Your API key should be included as the username. The password should be left empty.
This means you need to do something like the following:
$curl = curl_init();
$url = "http://whatever-this-should-be";
curl_setopt($curl, CURLOPT_URL, $url);
$username = "your-api-key-goes-here";
$password = ""; // leave this blank, as per the doc
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$res = curl_exec($curl);
You'll need to tweak other curl options to handle output, etc. but this is the core of what you'll need.
You might also want to check out this library if you want to do all of this at a slightly higher (i.e. easier) level.
UPDATE: Please be aware that this is just sample code. I wouldn't advocate storing your API key in your script file for production code!
I have made this code and it is working fine. Thank you all for answering my question.
$url = "https://api.postmates.com/v1/customers/my-customer-id/delivery_quotes";
$uname = "my-api-key";
$pwd = null;
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $uname . ":" . $pwd);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, "dropoff_address=20 McAllister St, San Francisco, CA 94102&pickup_address=101 Market St, San Francisco, CA 94105");
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($process);
curl_close($process);
var_dump($return);
This is driving me crazy so hoping for some help here.
I'm looking to get an authentication token after POSTing user information to an XML page using HTTP Basic Auth via PHP. The token is supposed to be returned back in XML. I'm using curl for this.
While I get absolutely no errors when I run the PHP, I am simply unable to get the token. I just get a blank page.
Here is my PHP code:
<?php
$username ='abc';
$password = 'xyz';
$user_token = base64_encode($username . '-' . $password);
$target_url = 'https://url_example/users/sign_in.xml';
$ch = curl_init($target_url);
curl_setopt($ch, CURLOPT_POST, true);
$headers = array('Authorization=Basic ' . $user_token);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$response_data = curl_exec($ch);
$xml = simplexml_load_string($response_data);
print_r($xml);
if (curl_errno($ch)> 0){
die('There was a cURL error: ' . curl_error($ch));
} else {
curl_close($ch);
}
?>
it should be : not -
try this
$process = curl_init("https://url_example/users/sign_in.xml");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $headers));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
I'm trying to connect to the basecamp api (json) using PHP + cURL, but all my attempts have failed, and I'm not sure why. The end goal is to build a dashboard that displays information from basecamp projects to team members over http.
<?php
$basecamp_url = 'https://basecamp.com/xxxxxx/api/v1';
$username = 'username';
$password = 'pass';
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $basecamp_url.'/projects.xml');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
$response = curl_exec($session);
curl_close($session);
echo '<pre>';
print_r($response);
?>
I am working on a very similar project as you are right now. You have to create a Basecamp app first. Than the following code should get you connected:
$appName = 'your app name';
$appContact = 'your app email;
$basecampAccountId = 'xxxxx';
$basecampUsername = 'youremailhere';
$basecampPassword = 'yourpassword here';
$baseUrl = "https://basecamp.com/$basecampAccountId/api/v1";
$url= $baseUrl.'/projects.json';
$credentials = "$basecampUsername:$basecampPassword";
$helloHeader = "User-Agent: $appName ($appContact)";
echo $url.'<br>';
echo $credentials.'<br>';
echo $helloHeader.'<br>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
print_r($response);