Pastebin API Paste Data - php

I've been trying to get the pastebin API to instead of telling me the pastebin link , just output the raw data. The PHP code is this :
<?php
$api_dev_key = 'Stackoverflow(fake key)';
$api_paste_code = 'API.'; // your paste text
$api_paste_private = '1'; // 0=public 1=unlisted 2=private
$api_paste_expire_date = 'N';
$api_paste_format = 'php';
$api_paste_code = urlencode($api_paste_code);
$url = 'http://pastebin.com/api/api_post.php';
$ch = curl_init($url);
?>
Normally this would upload the $api_paste_code into pastebin , showing up like pastebin.com/St4ck0v3RFL0W , but instead I want it to generate the raw data.
The raw data link is http://pastebin.com/raw.php?i= , can anyone help?
Reference : http://pastebin.com/api

As far as I see, the response contains the Pastebin URL generated when the content is created. An Url like this:
http://pastebin.com/UIFdu235s
So what you only need is to get rid of "http://pastebin.com/" doing:
$id = str_replace("http://pastebin.com/", "", $url_received_on_last_step);
And then, append it to the raw url you provided:
$url_raw = "http://pastebin.com/raw.php?i=".$id;
And you'll get the raw data.

First off, please note that you must send a POST request to the pastebin.com API, not GET. So don't use urlencode() on your input data!
To get the raw paste url from the page url, you have several options. But the easiest is probably:
$apiResonse = 'http://pastebin.com/ABC123';
$raw = str_replace('m/', 'm/raw.php?i=', $apiResponse);
Finally, here is a complete example:
<?php
$data = 'Hello World!';
$apiKey = 'xxxxxxx'; // get it from pastebin.com
$apiHost = 'http://pastebin.com/';
$postData = array(
'api_dev_key' => $apiKey, // your dev key
'api_option' => 'paste', // action to perform
'api_paste_code' => utf8_decode($data), // the paste text
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "{$apiHost}api/api_post.php",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($postData),
));
$result = curl_exec($ch); // on success, some string like 'http://pastebin.com/ABC123'
curl_close($ch);
if ($result) {
$pasteId = str_replace($apiHost, '', $result);
$rawLink = "{$apiHost}raw.php?i={$pasteId}";
echo "Created new paste.\r\n Paste ID:\t{$pasteId}\r\n Page Link:\t{$result}\r\n Raw Link:\t{$rawLink}\r\n";
}
Running the above code, outputs:
c:\xampp\htdocs>php pastebin.php
Created new paste.
Paste ID: Bb8Ehaa7
Page Link: http://pastebin.com/Bb8Ehaa7
Raw Link: http://pastebin.com/raw.php?i=Bb8Ehaa7

Related

How to output the contents of file_get_contents

I'm trying to figure out how to get a php file (html, css and javascript) and load it into the content section below.
The following is the original...
function wpse_124979_add_help_tabs() {
if ($screen = get_current_screen()) {
$help_tabs = $screen->get_help_tabs();
$screen->remove_help_tabs();
$screen->add_help_tab(array(
'id' => 'my_help_tab',
'title' => 'Help',
'content' => 'HTML CONTENT',
I have tried the following but fails. I added a file_get_contents (first line), and then tried pull it in with 'content' => $adminhelp,
The following is with my amended code...
$adminhelp = file_get_contents('admin-help.php');
function wpse_124979_add_help_tabs() {
if ($screen = get_current_screen()) {
$help_tabs = $screen->get_help_tabs();
$screen->remove_help_tabs();
$screen->add_help_tab(array(
'id' => 'my_help_tab',
'title' => 'WTV Help',
'content' => $adminhelp,
Any ideas what's wrong?
If you want the output of the PHP file to be saved as $adminhelp use:
$adminhelp = file_get_contents('http://YOUR_DOMAIN/admin-help.php');
Right now you're loading the source code of admin-help.php into $adminhelp.
Another example for getting the output of a webpage is cURL:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://YOUR_DOMAIN/admin-help.php");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$adminhelp = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
If this doesn't answer your question, please include the error message that you're receiving.

How to execute URL in codeigniter?

I want to send the SMS to the user using the third party API key in codeigniter. For that I used the file_get_content() But when file runs it takes the more execution time and after display blank page.
I also use CURL but same error in codeignator.
This code runs another PHP script only error in codeigniter
Following my code:
$to = "thisisprashantkumbhar";
$username ='puretechnology';
$numbers = '9326447272';
$messagenew=rawurlencode($to);
$apikey = 'gdfgrte5-er54-h57f-4rgt-0a7215d15abc';
$url = "http://sms1.businesslead.co.in/sendSMS?username=$username&message=$messagenew&sendername=CMISAM&smstype=TRANS&numbers=$numbers&apikey=$apikey";
$response = file_get_contents($url);
return $response;
$stream_options = array(
'http' => array(
'method' => 'GET',
),
);
$context = stream_context_create($stream_options);
$response = file_get_contents("http://api.smsbrain.in/1.2/appsms/send.php?user=username&passwd=141414&senderId=SATISH&recipients=9723613143&message=Hello", null, $context);
echo json_encode($response);
echo $url and it will show you the complete url then execute url you will get the error. e.g invalid key check your details as well.

POST url with Curl PHP

I have a thank you page with URL that contains variables :
http://vieillemethodecorpsneuf.com/confirmation-achat-1a/?item=1&cbreceipt=VM6JQ6VE&time=1429212702&cbpop=C123FA24&cbaffi=twitpalace&cname=Roberto+Laplante&cemail=roberto%40gmail.com&ccountry=FR&czip=000
I have this GET function to catch the variables :
<?php
$clickbank_name = (isset($_GET['cname'])) ? $_GET['cname'] : '';
$clickbank_email = (isset($_GET['cemail'])) ? $_GET['cemail'] : '';
$clickbank_country = (isset($_GET['ccountry'])) ? $_GET['ccountry'] : '';
$clickbank_zip = (isset($_GET['czip'])) ? $_GET['czip'] : '';
$clickbank_aff = (isset($_GET['cbaffi'])) ? $_GET['cbaffi'] : '';
?>
Now I need to use curl PHP to send the data to that Zapier URL (but with the variables attached to it so it will give me) :
https://zapier.com/hooks/catch/bheq6y/?tag=client&cbaffi=twitpalace&cname=Roberto+Laplante&cemail=roberto%40gmail.com&ccountry=FR&czip=000
ps. I have added a manual tag to the URL
What would be the PHP Curl code to make this work? Need to be behind the scene operation.
I'll give it a shot.
$get_fields = ['tag' => 'client'];
if (isset($_GET['cname'])) $get_fields['cname'] = $_GET['cname'];
if (isset($_GET['cemail'])) $get_fields['cemail'] = $_GET['cemail'];
if (isset($_GET['ccountry'])) $get_fields['ccountry'] = $_GET['ccountry'];
if (isset($_GET['czip'])) $get_fields['czip'] = $_GET['czip'];
if (isset($_GET['cbaffi'])) $get_fields['cbaffi'] = $_GET['cbaffi'];
$encoded = '';
foreach($get_fields as $name => $value){
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
$url = 'https://zapier.com/hooks/catch/bheq6y/?'.rtrim($encoded,'&');
// simple get curl
$output = file_get_contents($url);
// or if you want more control over the request
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
$output = curl_exec($curl);
curl_close($curl);
In this example, we are posting data in parameters to another curl.php page and in curl.php,
i wrote some code to get executed whenever curl.php get called and the result will be sent back to page from where curl.php got a request.
Mechanism : This example is made in PHP using Curl.
First Step : Create curl.php file. That file will be called by another php file by using curl mechanism.
So in curl.php we will get some parameters. We get parameters and do some functionalities. After that when we desire output,
we just encode with json using json_encode() and simple echo.
$post = $_POST;
echo json_encode($post);
Note : index.php will get the data in json format because we are sending back data in json format to index.php
Second Step : we have to create an index.php file. Where we write a logic for executiing a curl with some parameters and then call curl.php and get result from curl.php
$url = 'http://localhost/curl_demo/curl.php'; // This is my targeted file(curl.php) that i want to execute when curl executed
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'id=1&name=sanjay'); // pass parameters to curl.php
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$resArr = json_decode($response, true);
curl_close($ch);
print_r($resArr);
Note: The data that we get after curl executed successfully, we will get it in json format. That means we have to decode it using json_decode() in php.

cURL - How to getting last redirect address

i write some code in php.
I wanna get last redirecting adress on this is site:
fluege.de
I posting this is;
$dep= "sFlightInput[accDep]=ZRH";
$arr= "sFlightInput[accArr]=VIE";
$depregion= "sFlightInput[accDepRegion]=";
$arrregion= "sFlightInput[accArrRegion]=";
$multidep= "sFlightInput[accMultiAirportDep]=ZRH";
$multiarr= "sFlightInput[accMultiAirportArr]=ZRH";
$ftype = "sFlightInput[flightType]=RT";
$depcity = "sFlightInput[depCity]=Zürich+-+Flughafen+(ZRH)+-+Schweiz";
$arrcity = "sFlightInput[arrCity]=Wien+-+Internationaler+Flughafen+(VIE)+-+Österreich";
$sdate = "sFlightInput[departureDate]=29.03.2014";
$srange = "sFlightInput[departureTimeRange]=2";
$rdate ="sFlightInput[returnDate]=05.04.2014";
$rrange = "sFlightInput[returnTimeRange]=2";
$adt = "sFlightInput[paxAdt]=1";
$chd ="sFlightInput[paxChd]=0";
$inf = "sFlightInput[paxInf]=0";
$cabin = "sFlightInput[cabinClass]=Y";
$airline = "sFlightInput[depAirline]=";
$send = $dep.$arr.$depregion.$arrregion.$multidep.$multiarr.$ftype.$depcity.$arrcity.$sdate.$srange.$rdate.$rrange.$adt.$chd.$inf.$cabin.$airline;
I using this ;
echo getLastEffectiveUrl("http://www.fluege.de/flight/wait/".$send);
And there is function
function getLastEffectiveUrl($url)
{
// initialize cURL
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
));
// execute the request
$result = curl_exec($curl);
// fail if the request was not successful
if ($result === false) {
curl_close($curl);
return null;
}
// extract the target url
$redirectUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
return $redirectUrl;
}
They code must give this url;
www.fluege.de/wait/?accDep=&accArr=&accDepRegion=&accArrRegion=&accMultiAirportDep=&accMultiAirportArr=&flightType=RT&depCity=Z%FCrich+-+Flughafen+%28ZRH%29+-+Schweiz&arrCity=Wien+-+Internationaler+Flughafen+%28VIE%29+-+%D6sterreich&departureDate=04.04.2014&departureTimeRange=2&returnDate=20.04.2014&returnTimeRange=2&paxAdt=1&paxChd=0&paxInf=0&cabinClass=Y&depAirline=
But i need ;
http://www.fluege.de/flight/encodes/sFlightInput/5f8ccad612bafb69e7693f04cfaf1458/ (etc)
The code you provided does not handle cookies, so if the site you are query'ing requires this, your code won't work.
I checked http://php.net/manual/en/function.curl-setopt.php, but it seems like cURL cannot store cookies in memory. By adding the following line under curl_setopt_array, cookies are kept in a temporary file:
CURLOPT_COOKIEJAR => tempnam(sys_get_temp_dir(), 'cookiejar'),
However, I did not get your specific case to work. I noticed that the URL you create does not contain a question mark, and that the URL that your script creates does not redirect at all; it returns with 200 OK. I checked this using the following shell command:
curl -LI 'http://www.fluege.de/flight/wait/sFlightInput\[accDep\]=ZRHsFlightInput\[accArr\]=VIEsFlightInput\[accDepRegion\]=sFlightInput\[accArrRegion\]=sFlightInput\[accMultiAirportDep\]=ZRHsFlightInput\[accMultiAirportArr\]=ZRHsFlightInput\[flightType\]=RTsFlightInput\[depCity\]=Zürich+-+Flughafen+(ZRH)+-+SchweizsFlightInput\[arrCity\]=Wien+-+Internationaler+Flughafen+(VIE)+-+ÖsterreichsFlightInput\[departureDate\]=29.03.2014sFlightInput\[departureTimeRange\]=2sFlightInput\[returnDate\]=05.04.2014sFlightInput\[returnTimeRange\]=2sFlightInput\[paxAdt\]=1sFlightInput\[paxChd\]=0sFlightInput\[paxInf\]=0sFlightInput\[cabinClass\]=YsFlightInput\[depAirline\]='
If it's unclear what the URL should look like, you should contact fluege.de to ask them how to use their API.

Getting JSON response with PHP

I'm trying to connect an API that uses 0AUTH2 via PHP. The original plan was to use client-side JS, but that isn't possible with 0AUTH2.
I'm simply trying get a share count from the API's endpoint which is here:
https://api.bufferapp.com/1/links/shares.json?url=[your-url-here]&access_token=[your-access-key-here]
I do have a proper access_token that I am using to access the json file, that is working fine.
This is the code I have currently written, but I'm not even sure I'm on the right track.
// 0AUTH2 ACCESS TOKEN FOR AUTHENTICATION
$key = '[my-access-key-here]';
// JSON URL TO BE REQUESTED
$json_url = 'https://api.bufferapp.com/1/links/shares.json?url=http://bufferapp.com&access_token=' . $key;
// GET THE SHARE COUNT FROM THE REQUEST
$json_string = '[shares]';
// INITIALIZE CURL
$ch = curl_init( $json_url );
// CONFIG CURL OPTIONS
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
// SETTING CURL AOPTIONS
curl_setopt_array( $ch, $options );
// GET THE RESULTS
$result = curl_exec($ch); // Getting jSON result string
Like I said, I don't know if this is the best method - so I'm open to any suggestions.
I'm just trying to retrieve the share count with this PHP script, then with JS, spit out the share count where I need it on the page.
My apologies for wasting anyone's time. I have since been able to work this out. All the code is essentially the same - to test to see if you're getting the correct response, just print it to the page. Again, sorry to have wasted anyones time.
<?php
// 0AUTH2 ACCESS TOKEN FOR AUTHENTICATION
$key = '[your_access_key_here]';
// URL TO RETRIEVE SHARE COUNT FROM
$url = '[your_url_here]';
// JSON URL TO BE REQUESTED - API ENDPOINT
$json_url = 'https://api.bufferapp.com/1/links/shares.json?url=' . $url . ' &access_token=' . $key;
// GET THE SHARE COUNT FROM THE REQUEST
$json_string = '[shares]';
// INITIALIZE CURL
$ch = curl_init( $json_url );
// CONFIG CURL OPTIONS
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $json_string
);
// SETTING CURL AOPTIONS
curl_setopt_array( $ch, $options );
// GET THE RESULTS
$result = curl_exec($ch); // Getting jSON result string
print $result;
?>

Categories