Problem with confirming subscription pubsubhubbub - websub

The first code:
if(isset($_GET["hub_challenge"])) {
echo $_GET["hub_challenge"];
}
else {
}
$feeded = $_POST['feed'];
$ch = curl_init("http://pubsubhubbub.appspot.com");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,"hub.mode=subscribe&hub.verify=async&hub.callback=http://rssreaderbg.net/pubsubbub/example/index.php?url=$feeded&hub.topic=$feeded");
curl_exec($ch);
The callback code:
file_put_contents("logmeme.txt",$HTTP_RAW_POST_DATA,FILE_APPEND);
https://pubsubhubbub.appspot.com/topic-details?hub.url=http%3A%2F%2Frssreaderbg.net%2Fblog%2F%3Ffeed%3Drss2
https://pubsubhubbub.appspot.com/subscription-details?hub.callback=http%3A%2F%2Frssreaderbg.net%2Fpubsubbub%2Fexample%2Findex.php&hub.topic=http%3A%2F%2Frssreaderbg.net%2Fblog%2F%3Ffeed%3Drss2&hub.secret=
It can't reach the callback url or ?

Try explicitly setting the header to 2xx before echoing the challenge.
header('HTTP/1.1 204 "No Content"', true, 204);

Related

PHP + curl, HTTP POST to Open cart, add to cart section?

Can anyone show me how to do a PHP curl with an HTTP POST to open cart using API?
I want to send data like this:
product_id = 700, quantity = 1
To www.example.com
I expect the curl to return a response like {"success":"Success: You have modified your shopping cart!"} Are there any examples?
Do it the way as below:
<?php
$output = apirequest();
function apirequest()
{
$params['product_id'] = 700;
$params['quantity'] = 1;
$input = array('data' => json_encode($params));
$options = array(CURLOPT_RETURNTRANSFER=> true, //return web page
CURLOPT_HEADER=> false, //don't return headers
CURLOPT_AUTOREFERER=> true, //set referrer on redirect
CURLOPT_CONNECTTIMEOUT=> 180, //timeout on connect
CURLOPT_TIMEOUT=> 180, //timeout on response
CURLOPT_POST=> 1, //I am sending post data
CURLOPT_POSTFIELDS=> $input
);
$ch = curl_init("www.example.com");
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>

PAYPAL IPN Listner

I have been pulling my hair out trying to get this to work for over a week. I have even purchased an SSL cert for the domain.
I keep getting the error:
[2016-03-14 18:07 Europe/London] Can not connect to PayPal to validate IPN message: SSL connect error
Heres my code.
<?php
// CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
// Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
// Set this to 0 once you go live or don't require logging.
define("DEBUG", 1);
// Set to 0 once you're ready to go live
define("USE_SANDBOX", 1);
define("LOG_FILE", "ipn.log");
error_log(date('[Y-m-d H:i e] '). "Test message: " . PHP_EOL, 3, LOG_FILE);
// Read POST data
// reading posted data directly from $_POST causes serialization
// issues with array data in POST. Reading raw POST data from input stream instead.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// Post IPN data back to PayPal to validate the IPN data is genuine
// Without this step anyone can fake IPN data
if(USE_SANDBOX == true) {
$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
//$userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2';
$ch = curl_init($paypal_url);
if ($ch == FALSE) {
return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
// CONFIG: Optional proxy configuration
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
// Set TCP timeout to 30 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below. Ensure the file is readable by the webserver.
// This is mandatory for some environments.
$res = curl_exec($ch);
if (curl_errno($ch) != 0) // cURL error
{
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Can not connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
exit;
} else {
// Log the entire HTTP response if debug is switched on.
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
}
// Inspect IPN validation result and act accordingly
// Split response headers and payload, a better way for strcmp
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "VERIFIED") == 0) {
// check whether the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your PayPal email
// check that payment_amount/payment_currency are correct
// process payment and mark item as paid.
// assign posted variables to local variables
//$item_name = $_POST['item_name'];
//$item_number = $_POST['item_number'];
//$payment_status = $_POST['payment_status'];
//$payment_amount = $_POST['mc_gross'];
//$payment_currency = $_POST['mc_currency'];
//$txn_id = $_POST['txn_id'];
//$receiver_email = $_POST['receiver_email'];
//$payer_email = $_POST['payer_email'];
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
}
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
// Add business logic here which deals with invalid IPN messages
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
}
}
?>
I'm not sure why you bought an SSL certificate to fix connecting to PayPal.
Try changing
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
to
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
My bet is the SSL handshake is failing to the server. Disabling the checks should help clear it up.

outlook api giving error code request parameter missing

I am working on outlook api and i am getting the following error code in response
"error": {
"code": "request_parameter_missing",
"message": "The request entity body is missing a required parameter. The request must include at least one of these parameters: 'first_name', 'last_name', 'emails', 'employer'."
}
here is my code
header('Content-Type: application/json; charset=utf-8');
$access_token = TOKEN;
$api_url = "https://apis.live.net/v5.0/me/contacts?&access_token=".$access_token;
$curl = curl_init($api_url);
$curl_data = array(
'first_name' => "Roberto",
'last_name' => "Tamburello"
);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
echo"<pre>";
print_r($curl_response);
echo "</pre>";
I have been searching for few hours now and i cant seem to understand what am i doing wrong. Any help will be greatly appreciated
I was struggling with this too. For me the content type header wasn't set correctly to "application/json" and therefore even though the data was there, it didn't recognise it. Fixing this removed the error for me.
Not sure exactly where your problem is but thought this might help others. Here is my request in .net
var requestUrl = " https://apis.live.net/v5.0/me/contacts";
//set body data
var data = JsonConvert.SerializeObject(liveContact);
//liveContact is a custom object for storing contact data
byte[] byteArray = Encoding.UTF8.GetBytes(data);
WebRequest req = HttpWebRequest.Create(requestUrl);
req.Headers.Add("Authorization", "Bearer " + "...access token...");
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = byteArray.Length;
using (Stream dataStream = req.GetRequestStream()) {
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
using (WebResponse response = await req.GetResponseAsync()) {
using (var dataStream = response.GetResponseStream()) {
using (StreamReader reader = new StreamReader(dataStream)) {
string responseFromServer = reader.ReadToEnd();
}
}
}

Get users a Twitter user is following

I am using the Twitter API to create a web application. However, I would like to be able to get a list of users that a specific user is following. I have looked at the documentation and I have not been able to find how to do this.
For example, I might be trying to get the users that baconman is following. How can I do this using the Twitter API?
<?php
$screen_name = 'baconman';
$url = 'https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name='.$screen_name;
$list = curl($url,'GET');
$followers_ids = json_decode($list);
$user_detail = 'https://api.twitter.com/1/users/lookup.json?user_id='.implode(',',$followers_ids->ids).'&include_entities=true';
$details = curl($user_detail,'GET');
function curl($url, $method = 'get', $header = null, $postdata = null, $includeheader=FALSE, $timeout = 60)
{
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
if ($header)
curl_setopt($s,CURLOPT_HTTPHEADER, $header);
/*if ($this->debug)*/
curl_setopt($s,CURLOPT_VERBOSE, FALSE);
curl_setopt($s,CURLOPT_TIMEOUT, $timeout);
curl_setopt($s,CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($s,CURLOPT_MAXREDIRS, 3);
curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
curl_setopt($s,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($s,CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($s,CURLOPT_COOKIEFILE, 'cookie.txt');
if(strtolower($method) == 'post')
{
curl_setopt($s,CURLOPT_POST, true);
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
}
else if(strtolower($method) == 'delete')
{
curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'DELETE');
}
else if(strtolower($method) == 'put')
{
curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
}
curl_setopt($s,CURLOPT_HEADER, $includeheader);
//curl_setopt($s,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $html;
}

Apache Camel - Problems with Node.js

So I have 100 different things going wrong in a piece of code. I recently switched from Apache and PHP to Node.js. I have a particular piece of code that merely duplicates a request that it receives. Basically, my web app posts a JSON request to my server (PHP or Node.js), then the server sends the exact same request to my Apache Camel configuration. In PHP, this worked perfectly. When switching to Node.js, I'm getting errors from Apache Camel.
Specifically, I have a piece of code that sends the exact same request twice almost instantaneously (I know how to fix it, just bear with me). In PHP, this code would work fine. In Node.js, the first request works fine, but the second one fails. Apache Camel receives a request with an empty body the second time. And to be honest, I'm 100% clueless.
What exactly could be going wrong here? Logic tells me that if it worked with PHP but not Node (same Camel code) that it has to be an issue with Node.js. But I had to mention Camel just in case, because Camel does a few weird things with requests sometimes.
I'm going to post my code below and maybe you can see an issue. I've been working on this for 3 days (on and off) and haven't found a problem yet. Also, bear in mind that I just started using node a few days ago.
PHP
<?php
require_once("globals.php");
//There's nested JSON here so I could include the destination address
$request = file_get_contents('php://input');
$json = json_decode($request);
$urlid = $json->{"urlid"};
$json = $json->{"data"};
if (session_start() === FALSE)
{
echo "{ \"postsuccess\": false, \"error\": -1 }";
return;
}
if(!isset($_SESSION["username"]) ||
!isset($_SESSION["expirationdate"]) ||
!isset($_SESSION["securitytoken"]) ||
$_SESSION["expirationdate"] <= time())
{
echo "{ \"postsuccess\": false, \"error\": -2 }";
return;
}
if(strtolower($_SESSION["username"]) != strtolower($_COOKIE["UserNameCookie"]))
{
echo "{ \"postsuccess\": false, \"error\": -3 }";
return;
}
$json->{"securitytoken"} = strtolower($_SESSION["securitytoken"]);
$json->{"username"} = strtolower($_SESSION["username"]);
$request = json_encode($json);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL[$urlid]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain; charset=utf-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
Node.js (Typescript)
function handler(request: ExpressServerRequest, response: ExpressServerResponse) {
try {
var json = (request.body || JSON.parse(request.rawBody));
var security_token = request.cookies.get(global.security_token_cookie, {signed: true});
var username = request.cookies.get(global.username_cookie, {signed: true});
var urlid = json.urlid;
json = json.data;
if (!security_token || !username) {
response.send({postsuccess: false, error: -2});
return;
}
json.securitytoken = security_token;
json.username = username;
var callback = function(data) {
response.json(data);
};
db.databaseRequest(urlid, json, callback);
} catch (e) {
console.error(e.stack);
response.send({postsuccess: false, error: -1});
}
}
//Was in a different module
function databaseRequest(url: number, data: any, callback: (any) => void) {
try {
var json = JSON.stringify(data);
var headers = {
"Content-Type": "application/json",
"Content-Length": json.length
};
var options = {
host: db_host,
port: db_port,
path: url_locations[url],
headers: headers,
method: "POST"
};
var request = http.request(options, function(response) {
response.setEncoding("utf8");
var returnData = "";
response.on("data", function(d) {
returnData += d;
});
response.on("end", function() {
if (returnData) {
callback(JSON.parse(returnData));
} else {
callback(null);
}
});
});
request.on("error", function(e) {
console.error("Error posting database request:");
console.error("Location: " + url_locations[url]);
console.error("Data: " + json);
console.error("Error Event: " + e);
});
request.write(json);
console.log("Sending: " + json + " to " + url_locations[url]);
request.end();
} catch (e) {
console.error(e.stack);
request.end();
callback({ "dberror" : true });
}
}
EDIT:
I'm going to keep this updated, but I honestly doubt anybody but me will ever have this issue. :/
I found the root of the problem. It turned out to be a concurrency issue. PHP submitted the requests linearly, while Node.js is non-blocking, so it send requests before the others got back. Essentially, I was dumb enough to store state information in a processor. My routes look something like this:
from("jetty:http://foo").process(new WorkProcessor()).to("direct:foo2");
My initial thinking was that "OK, a new WorkProcessor is created every time the route is triggered, so each message gets its own little sandbox". Unfortunately, that's not the case, it's only created once. I don't know why I would think that, but I did. :(
So I cleared up the issue by not storing info in Processor instances. It made my code a little longer, but it solved my issue.