here i am trying to fetch all issues from Redmine by using curl php below is the code which i used.
my CURL.php:
flie class file in redmine/redmine_curl.php
<?php # Redmine Api
class class_redmine{
function get_upload_token($filecontent){
global $redmine_url , $redmine_key;
$upload_url = $redmine_url.'uploads.json?key='.$redmine_key;
$request['type'] = 'post';
$request['content_type'] = 'application/octet-stream';
//$filecontent = file_get_contents('test.php');
return $token = $this->curl_redmine($upload_url,$request,$filecontent);
//$token->upload->token;
}
#Issue
function create_issue($post_data){
global $redmine_url , $redmine_key;
$issue_url = $redmine_url.'issues.json?key='.$redmine_key;
$request['type'] = 'post';
$request['content_type'] = 'application/json';
return $this->curl_redmine($issue_url,$request,$post_data);
}
function get_issue($issue_id='',$project_id=''){
global $redmine_url , $redmine_key;
if($project_id!=''){
$issue_url = $redmine_url.'issues.json?key='.$redmine_key.'&project_id='.$project_id;
}else{ $issue_url = ($issue_id=='')?$redmine_url.'issues.json?key='.$redmine_key : $redmine_url.'issues/'.$issue_id.'.json?key='.$redmine_key;
}
return $this->curl_redmine($issue_url,'','');
}
#Projects
function get_projects($project_id=''){
global $redmine_url , $redmine_key;
$proj_url = ($project_id=='')?$redmine_url.'projects.json?key='.$redmine_key : $redmine_url.'projects/'.$project_id.'.json?key='.$redmine_key;
return $this->curl_redmine($proj_url,'','');
}
#Curl
function curl_redmine($redmine_url,$request='',$post_data=''){
if(!isset($request['type'])){ $request['type']=null; }
if(!isset($request['content_type'])){ $request['content_type']=null; }
//Create a curl object
$ch = curl_init();
//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $redmine_url );
if($request['type'] == 'post'){
//This is a POST query
curl_setopt($ch, CURLOPT_POST,1);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: '.$request['content_type'],
'Content-Length: ' . strlen($post_data))
);
}
//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
//Execute the action to login
$postResult = curl_exec($ch);
//if($postResult == false){ return $info = curl_getinfo($ch);}
$response = json_decode($postResult);
//echo '<pre>'; print_r($response); echo '</pre>';
return $response;
}
}//class_redmine
?>
here is my example.php
<?php
function get_redmine($methodName='',$data=''){
global $redmine_url , $redmine_key;
$redmine_url = 'http://192.168.12.231:80/';
$redmine_key = 'API KEY';
include_once('curlcall.php');
$obj_redmine = new class_redmine();
#check Auth
$res = $obj_redmine->get_projects();
if(!isset($res->projects) || (isset($res->total_count) && ($res->total_count)==0)){ return -1; }
switch($methodName){
case 'check_status' : return $login_integrate;
##check redmine integration in vision break;
##Project
case 'projectAll' : return $obj_redmine->get_projects(); #used
break;
case 'projectById' : return $obj_redmine->get_projects($data['project_id']);
break;
##Issues
case 'issueAll' : return $obj_redmine->get_issue();
break;
case 'issueByProjectId' : return $obj_redmine->get_issue('',$data['project_id']);
break;
case 'uploadFileToIssue' : return $obj_redmine->get_upload_token($data);
break;
default: return 0;
}
}
#get all issue
$res = get_redmine('issueAll');
echo '<pre>';print_r($res);
?>
This code is giving only 25 records.at the end of my output it is saying like
total:201
offset:0
limit:25
I can't understand how to get all issues please help anybody, i am new for php.
the answer is all in the API docs, if you'd just bother to read it. use the limit parameter to get more results, and the offset parameter to load more
/issues.json?offset=0&limit=100 would fetch the first 100 issues
/issues.json?offset=100&limit=100 would fetch the next 100 issues, and so on.
... but the first thing you should do is get your code formatted, because that unformatted thing is very annoying to read. then get rid of this line 'Content-Length: ' . strlen ( $post_data ) because curl will add that header automatically if you don't, and curl won't make any typos, nor will it calculate the length incorrectly, unlike us the progammers (curl has automated tests ran on every commit to make sure content-length is correct, i bet that's not part of your test suite), then get rid of this
if (! isset ( $request ['content_type'] )) {
$request ['content_type'] = null;
}
because it should never be null. for future reference, if you literally have no idea what it's supposed to be, set it to application/octet-stream, but even that is wrong in this case, because the API docs says the only supported POST encodings are application/json and application/xml, so the correct thing to do here would be:
if (isset ( $request ['content_type'] ) && $request ['content_type'] !== 'application/json' && $request ['content_type'] !== 'application/xml') {
throw new \InvalidArgumentException ( 'invalid content-type specified. supported types are "application/json" and "application/xml"' );
}
if($request ['type'] === 'post' && !isset($request ['content_type'])){
throw new \LogicException('no content-type was specified for this POST request, which is required. supported types are "application/json" and "application/xml"');
}
as for fetching all the issues, make a request first to see how many issues there are, then make a second request with limit=number_of_issues, like this
if (! $issue_id) {
// first we check how many issues there are (OPTIMIZEME: there is probably a more efficient way to do this, for example limit=0 ?)
$issues_url .= '&limit=1';
$number_of_issues = $this->curl_redmine ( $issue_url, '', '' ) ['total_count'];
$issue_url = substr ( $issues_url, 0, - 1 ) . $number_of_issues;
}
putting it all together, and you get this
<?php
// Redmine Api
class class_redmine {
function get_upload_token($filecontent) {
global $redmine_url, $redmine_key;
$upload_url = $redmine_url . 'uploads.json?key=' . $redmine_key;
$request ['type'] = 'post';
$request ['content_type'] = 'application/octet-stream';
// $filecontent = file_get_contents('test.php');
return $token = $this->curl_redmine ( $upload_url, $request, $filecontent );
// $token->upload->token;
}
// Issue
function create_issue($post_data) {
global $redmine_url, $redmine_key;
$issue_url = $redmine_url . 'issues.json?key=' . $redmine_key;
$request ['type'] = 'post';
$request ['content_type'] = 'application/json';
return $this->curl_redmine ( $issue_url, $request, $post_data );
}
function get_issue($issue_id = '', $project_id = '') {
global $redmine_url, $redmine_key;
if ($project_id != '') {
$issue_url = $redmine_url . 'issues.json?key=' . $redmine_key . '&project_id=' . $project_id;
} else {
$issue_url = ($issue_id == '') ? $redmine_url . 'issues.json?key=' . $redmine_key : $redmine_url . 'issues/' . $issue_id . '.json?key=' . $redmine_key;
}
if (! $issue_id) {
// first we check how many issues there are (OPTIMIZEME: there is probably a more efficient way to do this, for example limit=0 ?)
$issues_url .= '&limit=1';
$number_of_issues = $this->curl_redmine ( $issue_url, '', '' ) ['total_count'];
$issue_url = substr ( $issues_url, 0, - 1 ) . $number_of_issues;
}
return $this->curl_redmine ( $issue_url, '', '' );
}
// Projects
function get_projects($project_id = '') {
global $redmine_url, $redmine_key;
$proj_url = ($project_id == '') ? $redmine_url . 'projects.json?key=' . $redmine_key : $redmine_url . 'projects/' . $project_id . '.json?key=' . $redmine_key;
return $this->curl_redmine ( $proj_url, '', '' );
}
// Curl
function curl_redmine($redmine_url, $request = '', $post_data = '') {
if (! isset ( $request ['type'] )) {
$request ['type'] = null;
}
if (isset ( $request ['content_type'] ) && $request ['content_type'] !== 'application/json' && $request ['content_type'] !== 'application/xml') {
throw new \InvalidArgumentException ( 'invalid content-type specified. supported types are "application/json" and "application/xml"' );
}
if ($request ['type'] === 'post' && ! isset ( $request ['content_type'] )) {
throw new \LogicException ( 'no content-type was specified for this POST request, which is required. supported types are "application/json" and "application/xml"' );
}
// Create a curl object
$ch = curl_init ();
// Set the useragent
$agent = $_SERVER ["HTTP_USER_AGENT"];
curl_setopt ( $ch, CURLOPT_USERAGENT, $agent );
// Set the URL
curl_setopt ( $ch, CURLOPT_URL, $redmine_url );
if ($request ['type'] == 'post') {
// This is a POST query
curl_setopt ( $ch, CURLOPT_POST, 1 );
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// Set the post data
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-Type: ' . $request ['content_type']
) );
}
// We want the content after the query
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
// Follow Location redirects
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
/*
* Set the cookie storing files
* Cookie files are necessary since we are logging and session data needs to be saved
*/
// curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
// curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
// Execute the action to login
$postResult = curl_exec ( $ch );
// if($postResult == false){ return $info = curl_getinfo($ch);}
$response = json_decode ( $postResult );
// echo '<pre>'; print_r($response); echo '</pre>';
return $response;
}
}//class_redmine
(which is completely untested on my part, by the way.)
another issue you should fix is that your curl code is executed completely without error detection. for example, curl_setopt returns bool(false) if there was a problem setting your option, and curl_exec() returns bool(false) if there was a problem with during the transfer, add error detection for that. these functions should make that rather easy:
function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
$ret=curl_setopt($ch,$option,$value);
if($ret!==true){
//option should be obvious by stack trace
throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
}
return true;
}
function ecurl_exec ( /*resource*/$ch):bool{
$ret=curl_exec($ch);
if($ret===false){
throw new RuntimeException ( 'curl_exec() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
}
return $ret;
}
function return_var_dump(/*...*/){
$args = func_get_args ();
ob_start ();
call_user_func_array ( 'var_dump', $args );
return ob_get_clean ();
}
and to be sure you're not leaking any memory, you can wrap it all around a try{...curl...}finally{curl_close($ch);}, making sure curl_close would always be called, regardless of which curl function throws an exception. (this prevents memory leaks)
Related
Can be found there many resources on Internet with the same code and instructions about how to show your MailChimp subscriber count in WordPress. I used that code without problems until today, when received an PHP Warning:
PHP Warning: Missing argument 2 for Mailchimp::call(), called in
/.../mc-subscriber-count/mc-subscriber-count.php on line 19 and
defined in /.../mc-subscriber-count/Mailchimp.php on line 192
The mc-subscriber-count.php full code:
function wpb_mc_sub_count() {
include "Mailchimp.php";
$lastRunLog = __DIR__ . '/logs/lastrun.log';
$subfile = __DIR__ . '/logs/subcount.log';
$lastRun = file_get_contents( $lastRunLog );
if ( time() - $lastRun >= 86400 ) {
$MailChimp = new MailChimp( 'Your_MailChimp_API_Key' );
$mc = $MailChimp->call( 'lists/list' ); // THE LINE 19
/*****************************************************/
$subscriber_count .= $mc[data][0][stats][member_count];
file_put_contents( $lastRunLog, time() );
file_put_contents( $subfile, $subscriber_count );
} else {
$subscriber_count .= file_get_contents( $subfile );
}
return number_format( $subscriber_count );
}
add_shortcode( 'mc-subscribers', 'wpb_mc_sub_count' );
add_filter( 'widget_text', 'do_shortcode' );
The Mailchimp.php code (only the function from the line 192 - full code here):
public function call($url, $params) {
$params['apikey'] = $this->apikey;
$params = json_encode($params);
$ch = $this->ch;
curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
$start = microtime(true);
$this->log('Call to ' . $this->root . $url . '.json: ' . $params);
if($this->debug) {
$curl_buffer = fopen('php://memory', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
}
$response_body = curl_exec($ch);
$info = curl_getinfo($ch);
$time = microtime(true) - $start;
if($this->debug) {
rewind($curl_buffer);
$this->log(stream_get_contents($curl_buffer));
fclose($curl_buffer);
}
$this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
$this->log('Got response: ' . $response_body);
if(curl_error($ch)) {
throw new Mailchimp_HttpError("API call to $url failed: " . curl_error($ch));
}
$result = json_decode($response_body, true);
if(floor($info['http_code'] / 100) >= 4) {
throw $this->castError($result);
}
return $result;
}
How to solve that warning?
UPDATE
I forgot to mention that I see that there is missing a second argument, but I don't understand what this second argument can be.
P.S. I am not a PHP coder, so don't beat me.
After the received comments I analysed repeatedly the above two functions and decided to add as a second parameter to the line 19 the `$params` variable, so it looks now like this:
$mc = $MailChimp->call( 'lists/list', $params );
I don't know if this is the correct way, but I will wait now for errors, if will be some there.
UPDATE
Because I received here only downvotes instead of help (but it doesn't matter) and my first solution doesn't worked (see the deleted text), I googled again and finally I found a better one (I hope) to display the MailChimp subscribers count in Wordpress. I just wrapped the recommended code in a shortcode like this:
function wpb_mc_sub_count() {
$api['key']= 'INSERT-YOUR-API-KEY-HERE';
$url='https://INSERT-YOUR-US-DOMAIN-KEY.api.mailchimp.com/3.0//lists/INSERT-YOUR-LISTID-HERE/?apikey=INSERT-YOUR-API-KEY-HERE';
$lastRunLog = '/logs/lastrun.log';
$subfile = '/logs/subcount.log';
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 3600) {
// it's been more than one hour so we will connect to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
$total= $json['stats']['member_count'];
// update lastrun.log with current time
file_put_contents($lastRunLog, time());
file_put_contents($subfile, $total);
} else {
$total = file_get_contents($subfile);
}
//echo $total;
return $total;
}
add_shortcode( 'mc-subscribers', 'wpb_mc_sub_count' );
//add_filter( 'widget_text', 'do_shortcode' );
Just put the [mc-subscribers] shortcode where you want to display your MailChimp subscribers count.
I'm trying to write a vat number validation function which checks a vat number to see if it's valid.
Currently i've got this
function tep_check_vatnum($countries_id, $vatnum) {
$country = tep_get_countries_with_iso_codes($countries_id);
$iso = $country['countries_iso_code_2'];
if($iso == "GB"){
return false;
} else {
$url = "http://isvat.appspot.com/$iso/$vatnum";
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
// curl_setopt($session, CURLOPT_HTTPGET, 1);
// curl_setopt($session, CURLOPT_HEADER, false);
// curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
//echo "<!-- NOTE:" . $response . "-->";
curl_close($session);
if($response == "true"){
return true;
} else {
return false;
}
}
}
The service i'm using http://isvat.appspot.com is working, But it's not working.
EDIT
Hmm no luck. By not working, it keeps giving me an error saying the vat number isn't valid. On the create account form i've got this code.
<?php echo tep_draw_input_field('vatnum', '', 'class="input"') . ' ' . (tep_not_null(ENTRY_COMPANY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COMPANY_TEXT . '</span>': ''); ?>
if (is_numeric($country) == false) {
$error = true;
$messageStack->add('create_account', ENTRY_COUNTRY_ERROR);
} else {
if(strlen($vatnum) != 0){
if(is_numeric($vatnum) == false || tep_check_vatnum($country, $vatnum) == false){
$error = true;
$messageStack->add('create_account', "VAT Number is not a valid EU, non-UK VAT no.");
}
}
I guess there might be a problem invoking cURL. The good news is, you probably don't need to. ;) Give this a try:
function tep_check_vatnum($countries_id, $vatnum) {
$country = tep_get_countries_with_iso_codes($countries_id);
$iso = $country['countries_iso_code_2'];
if($iso == "GB"){
return false;
} else {
$url = "http://isvat.appspot.com/$iso/$vatnum";
$context = stream_context_create(array(
'http' => array(
'proxy' => 'http://PROXY_IP:PROXY_PORT'
),
));
$fp = fopen($url,'r',$context);
$response = fgets($fp);
fclose($fp);
//echo "<!-- NOTE:" . $response . "-->";
if($response == "true"){
return true;
} else {
return false;
}
}
}
It should work even if you don't have the cURL extension enabled.
Edit: As Michas said, it is difficult to find the problem with your original code without knowing what you mean by "not working". You could try to get some more information about the problem by stating error_reporting(E_ALL) or curl_error($session).
Possible causes: cURL is not installed or configured correctly or a proxy or firewall is blocking your request. While the code I suggested would eliminate the first one, the second one might also apply using it, depending on your settings.
Edit 2: Added simple proxy configuration
I am trying to search (filter) for files in a Dropbox folder, but no files are being found when there are files that match the filter. I am not using the PHP library provided by Dropbox.
Here is an extract of the code:
class Dropbox {
private $headers = array();
private $authQueryString = "";
public $SubFolders = array();
public $Files = array();
function __construct() {
$this->headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.DROPBOX_APP_KEY.'", oauth_token="'.DROPBOX_OAUTH_ACCESS_TOKEN.'", oauth_signature="'.DROPBOX_APP_SECRET.'&'.DROPBOX_OAUTH_ACCESS_SECRET.'"');
$this->authQueryString = "oauth_consumer_key=".DROPBOX_APP_KEY."&oauth_token=".DROPBOX_OAUTH_ACCESS_TOKEN."&oauth_signature_method=PLAINTEXT&oauth_signature=".DROPBOX_APP_SECRET."%26".DROPBOX_OAUTH_ACCESS_SECRET."&oauth_version=1.0";
}
public function GetFolder($folder, $fileFilter = "") {
//Add the required folder to the end of the base path for folder call
if ($fileFilter == "")
$subPath = "metadata/sandbox";
else
$subPath = "search/sandbox";
if (strlen($folder) > 1) {
$subPath .= (substr($folder, 0, 1) != "/" ? "/" : "")
.$folder;
}
//Set up the post parameters for the call
$params = null;
if ($fileFilter != "") {
$params = array(
"query" => $fileFilter
);
}
//Clear the sub folders and files logged
$this->SubFolders = array();
$this->Files = array();
//Make the call
$content = $this->doCall($subPath, $params);
//Log the files and folders
for ($i = 0; $i < sizeof($content->contents); $i++) {
$f = $content->contents[$i];
if ($f->is_dir == "1") {
array_push($this->SubFolders, $f->path);
} else {
array_push($this->Files, $f->path);
}
}
//Return the content
return $content;
}
private function doCall($urlSubPath, $params = null, $filePathName = null, $useAPIContentPath = false) {
//Create the full URL for the call
$url = "https://api".($useAPIContentPath ? "-content" : "").".dropbox.com/1/".$urlSubPath;
//Initialise the curl call
$ch = curl_init();
//Set up the curl call
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($params != null)
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$fh = null;
if ($filePathName != null) {
$fh = fopen($filePathName, "rb");
curl_setopt($context, CURLOPT_BINARYTRANSFER, true);
curl_setopt($context, CURLOPT_INFILE, $fh);
curl_setopt($context, CURLOPT_INFILESIZE, filesize($filePathName));
}
//Excecute and get the response
$api_response = curl_exec($ch);
if ($fh != null)
fclose($fh);
//Process the response into an array
$json_response = json_decode($api_response);
//Has there been an error
if (isset($json_response->error )) {
throw new Exception($json_response["error"]);
}
//Send the response back
return $json_response;
}
}
I then call the GetFolder method of Dropbox as such:
$dbx = new Dropbox();
$filter = "MyFilter";
$dbx->GetFolder("MyFolder", $filter);
print "Num files: ".sizeof($dbx->Files);
As I am passing $filter into GetFolder, it uses the search/sandbox path and creates a parameter array ($params) with the required query parameter in it.
The process works fine if I don't provide the $fileFilter parameter to GetFolder and all files in the folder are returned (uses the metadata/sandbox path).
Other methods (that are not in the extract for brevity) of the Dropbox class use the $params feature and they to work fine.
I have been using the Dropbpox API reference for guidance (https://www.dropbox.com/developers/core/docs#search)
At first glance, it looks like you're making a GET request to /search but passing parameters via CURLOPT_POSTFIELDS. Try using a POST or encoding the search query as a query string parameter.
EDIT
Below is some code that works for me (usage: php search.php <term>). Note that I'm using OAuth 2 instead of OAuth 1, so my Authorization header looks different from yours.
<?php
$access_token = '<REDACTED>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/1/search/auto');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:Bearer ' . $access_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('query' => $argv[1]));
$api_response = curl_exec($ch);
echo "Matching files:\n\t" . join("\n\t",
array_map(function ($file) {
return $file['path'];
}, json_decode($api_response, true)))."\n";
?>
http://benalman.com/code/projects/php-simple-proxy/examples/simple/
I am exactly following above Blog for Using PHP Proxy setting for Cross Domain. I am using XHR. I am able to successful to use GET method. But While using POST I am getting error CODE 200 and Empty XML in reply object.
However when i am using the simple XHR Code without phpproxy with below setting of google. chrome.exe --disable-web-security. I am successful for GET and POST both.
I am sure i am wrong somewhere in XHR.Send(Mydata). But if i was wrong in this method than i could not have been able to send success full post method.
Please help. I am novice in PHP i am sure i am missing something in PHP code that would enable me to post successfull. Below is crux of PHP code.
$enable_jsonp = true;
$enable_native = false;
$valid_url_regex = '/.*/';
$url = $_GET['url'];
if (!$url)
{
// Passed url not specified.
$contents = 'ERROR: url not specified';
$status = array(
'http_code' => 'ERROR'
);
}
else if (!preg_match($valid_url_regex, $url)) {
// Passed url doesn't match $valid_url_regex.
$contents = 'ERROR: invalid url';
$status = array(
'http_code' => 'ERROR'
);
}
else
{
$ch = curl_init($url);
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
}
if ($_GET['send_cookies'])
{
$cookie = array();
foreach ($_COOKIE as $key => $value)
{
$cookie[] = $key . '=' . $value;
}
if ($_GET['send_session'])
{
$cookie[] = SID;
}
$cookie = implode('; ', $cookie);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT']);
list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', curl_exec($ch), 2);
$status = curl_getinfo($ch);
curl_close($ch);
}
// Split header text into an array.
$header_text = preg_split('/[\r\n]+/', $header);
if ($_GET['mode'] == 'native')
{
if (!$enable_native)
{
$contents = 'ERROR: invalid mode';
$status = array(
'http_code' => 'ERROR'
);
}
// Propagate headers to response.
foreach ($header_text as $header)
{
if (preg_match('/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header))
{
header($header);
}
}
print $contents;
}
else
{
// $data will be serialized into JSON data.
$data = array();
// Propagate all HTTP headers into the JSON data object.
if ($_GET['full_headers'])
{
$data['headers'] = array();
foreach ($header_text as $header)
{
preg_match('/^(.+?):\s+(.*)$/', $header, $matches);
if ($matches)
{
$data['headers'][$matches[1]] = $matches[2];
}
}
}
// Propagate all cURL request / response info to the JSON data object.
if ($_GET['full_status'])
{
$data['status'] = $status;
}
else
{
$data['status'] = array();
$data['status']['http_code'] = $status['http_code'];
}
// Set the JSON data object contents, decoding it from JSON if possible.
$decoded_json = json_decode($contents);
$data['contents'] = $decoded_json ? $decoded_json : $contents;
// Generate appropriate content-type header.
$is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
header('Content-type: application/' . ($is_xhr ? 'json' : 'x-javascript'));
// Get JSONP callback.
$jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null;
// Generate JSON/JSONP string`enter code here`
$json = json_encode($data);
print $jsonp_callback ? "$jsonp_callback($json)" : $json;
}
I'm trying to do a very basic setup of LinkedIn - accessing public profile data about people.
I've tried several ways of accessing with oAuth over several hours. I'm not getting anywhere.
All the existing class structures et'al seem to help with accessing a user's account to post, or add friends, find a fish etc. I don't need any of that. I just want to get basic profile data.
Some code of latest attempts; but I don't get it past here:-
$consumer = new OAuthConsumer($apiKey, $apiSecret);
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $linkedInURL . "/uas/oauth/requestToken");
$req_req->sign_request($signature_method, $consumer, NULL);
$signed_url = $req_req->to_url();
That should gives me a signed request of:-
https://api.linkedin.com/uas/oauth/requestToken?oauth_consumer_key=xxx&oauth_nonce=xx&oauth_signature=xxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1316530337&oauth_version=1.0
Obviously, that's not what I need to get data. But, just out of interest I checked the URL with the API data request, as such:-
http://api.linkedin.com/v1/people/url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fchrisvoss:public?oauth_consumer_key=xxx&oauth_nonce=xxx&oauth_signature=xxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1316529463&oauth_version=1.0
And got :-
<error>
<status>401</status>
<timestamp>1316531835564</timestamp>
<request-id>L8A1M85MWN</request-id>
<error-code>0</error-code>
<message>
[unauthorized].OAU:tm6i3ke827xz|*01|*01|*01:1316529463:MSFHS3f4iaG9pg2gWYlf22W4NPo=
</message>
</error>
I'm just a bit clueless here. I know everyone says it's tears to implement oAuth and Linkedin. But, I don't need half of what most need, so how do I get to the basic data is only my question.
Thanks in advance for any help.
Try To Use the following code
session_start();
require_once("OAuth.php");
$domain = "https://api.linkedin.com/uas/oauth";
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_consumer = new OAuthConsumer("DEHYU99peS88wDDAFOcSm3Af5VO1tdrdgq1xPu_fpSSjsqPcoeABUs_NCyY33WIH", "gZrZr2-7s80CEsGpAHqFgREMbRWkR3L8__tkje3j-oKtIDlmn5KCR6bXD8i0HFp1", NULL);
$callback = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?action=getaccesstoken";
# First time through, get a request token from LinkedIn.
if (!isset($_GET['action'])) {
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "POST", $domain . "/requestToken");
$req_req->set_parameter("oauth_callback", $callback); # part of OAuth 1.0a - callback now in requestToken
$req_req->sign_request($sig_method, $test_consumer, NULL);
$ch = curl_init();
// make sure we submit this as a post
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
$req_req->to_header()
));
curl_setopt($ch, CURLOPT_URL, $domain . "/requestToken");
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
curl_close($ch);
//print_r($req_req); //<---- add this line
//print("$output\n"); //<---- add this line
parse_str($output, $oauth);
# pop these in the session for now - there's probably a more secure way of doing this! We'll need them when the callback is called.
$_SESSION['oauth_token'] = $oauth['oauth_token'];
$_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
# Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
Header('Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']);
#print 'Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']; // <---- add this line
} else {
# this is called when the callback is invoked. At this stage, the user has authorised the token.
# Now use this token to get a real session token!
//print "oauth_token = [[".$_REQUEST['oauth_token']."]]\n";echo "<br/><br/>";
$req_token = new OAuthConsumer($_REQUEST['oauth_token'], $_SESSION['oauth_token_secret'], 1);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "POST", $domain . '/accessToken');
$acc_req->set_parameter("oauth_verifier", $_REQUEST['oauth_verifier']); # need the verifier too!
$acc_req->sign_request($sig_method, $test_consumer, $req_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
$acc_req->to_header()
));
curl_setopt($ch, CURLOPT_URL, $domain . "/accessToken");
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error 1: ' . curl_error($ch);
}
curl_close($ch);
parse_str($output, $oauth);
$_SESSION['oauth_token'] = $oauth['oauth_token'];
$_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
# Now you have a session token and secret. Store these for future use. When the token fails, repeat the above process.
//$endpoint = "http://in.linkedin.com/in/intercom"; # need a + symbol here.
$endpoint = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,industry,educations,site-standard-profile-request)";
//$req_token = new OAuthConsumer($oauth['oauth_token'], $oauth['oauth_token_secret'], 1);
$req_token = new OAuthConsumer($oauth['oauth_token'],$oauth['oauth_token_secret'], 1);
//$profile_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "GET", $endpoint, array("name" => "intercom")); # but no + symbol here!
$profile_req = OAuthRequest::from_consumer_and_token($test_consumer,$req_token, "GET", $endpoint, array());
$profile_req->sign_request($sig_method, $test_consumer, $req_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
$profile_req->to_header()
));
curl_setopt($ch, CURLOPT_URL, $endpoint);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error 2: ' . curl_error($ch);
}
curl_close($ch);
//header ("Content-Type:text/xml");
//print $output;
$myFile = $_SERVER['DOCUMENT_ROOT']."/oauth/linkedin.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
//$stringData = "Bobby Bopper\n";
fwrite($fh, $output);
fclose($fh);
//Initialize the XML parser
global $currentTag;
global $profileArray;
$parser=xml_parser_create();
//Function to use at the start of an element
function start($parser,$element_name,$element_attrs) {
$element_name = strtolower($element_name);
global $currentTag;
$currentTag = $element_name;
/*switch($element_name) {
case "person":
$currentTag = $element_name;
break;
case "headline":
echo "headline: ";
break;
case "school-name":
echo "school-name: ";
break;
case "degree":
echo "degree: ";
break;
case "field-of-study":
echo "field-of-study: ";
}*/
}
//Function to use at the end of an element
function stop($parser,$element_name) {}
//Function to use when finding character data
function char($parser,$data){
//echo $data;
global $currentTag;
global $profileArray;
switch($currentTag) {
/* case "member-url":
if(!isset($profileArray['member-url'])) {
$profileArray['member-url'] = $data;//echo $profileArray['industry'];
}
break;*/
case "id":
if(!isset($profileArray['id'])) {
$profileArray['id'] = $data;//echo $profileArray['industry'];
}
break;
case "site-standard-profile-request":
if(!isset($profileArray['site-standard-profile-request'])) {
$profileArray['site-standard-profile-request'] = $data;//echo $profileArray['industry'];
}
break;
case "first-name":
if(!isset($profileArray['first-name'])) {
$profileArray['first-name'] = $data;//echo $profileArray['industry'];
}
break;
case "last-name":
if(!isset($profileArray['last-name'])) {
$profileArray['last-name'] = $data;//echo $profileArray['industry'];
}
break;
case "industry":
if(!isset($profileArray['industry'])) {
$profileArray['industry'] = $data;//echo $profileArray['industry'];
}
break;
case "headline":
if(!isset($profileArray['headline'])) {
$profileArray['headline'] = $data;
}
break;
case "school-name":
if(!isset($profileArray['school-name'])) {
$profileArray['school-name'] = $data;
}
break;
case "degree":
if(!isset($profileArray['degree'])) {
$profileArray['degree'] = $data;
}
break;
case "field-of-study":
if(!isset($profileArray['field-of-study'])) {
$profileArray['field-of-study'] = $data;
}
break;
}
}
//Specify element handler
xml_set_element_handler($parser,"start","stop");
//Specify data handler
xml_set_character_data_handler($parser,"char");
//Open XML file
$fp=fopen($_SERVER['DOCUMENT_ROOT']."/oauth/linkedin.xml","r");
//Read data
while ($data=fread($fp,4096)) {
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
//Free the XML parser
xml_parser_free($parser);
print_r($profileArray);
getCurrentCookieValue($name)
}
you can use linkedIn javascript API to retrieve profile information