Webapplication stops running code after calling function from another file - php

My web app stops when it tries executing a function from another file. I am using a include_once to include the file where the function is. I am also pretty sure that the path is correct. (if there is anyway to check let me know). the functions which I am trying to use are written in the class.connections.php . Also as you can see I am echoing 2 test. Only the first one shows.
Invite.php
global $page_array ;
global $current_user ;
include_once(DITC_PATH."/class.friends.php");
include_once(DITC_PATH."/class.connections.php");
$process_status = $current_user->process_hotmail_contacts ;
global $page_array ;
if($page_array['url']['3'] == 'hotmail'){
$process_source = 'hotmail' ;
};
if($_GET['code']){
echo "Test 1 - ";
$access_token_json = get_hotmail_access_token();
echo "Test 2 - ";
if($access_token_json){
echo 'test';
update_user_meta($current_user->ID, 'hotmail_access_token', $access_token_json);
$taskurl = "/tasks";
$data = array();
$data['type'] = 'process_hotmail_contacts';
$data['lang'] = get_site_language();
$data['access_token'] = $access_token_json ;
$data['user_id'] = $current_user->ID ;
$options = array();
add_task($taskurl, $data, $options);
$process_status = 'busy' ;
$process_source = 'hotmail' ;
};
}
$ditc_title = _t("Invite connections") ;
get_header();
function in class.connections
function get_hotmail_access_token(){
global $hotmail_creds;
$fields=array(
'code'=> urlencode($hotmail_creds['auth_code']),
'client_id'=> urlencode($hotmail_creds['client_id']),
'client_secret'=> urlencode($hotmail_creds['client_secret']),
'redirect_uri'=> urlencode($hotmail_creds['redirect_uri']),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl, CURLOPT_POST,5);
curl_setopt($curl, CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$access_token_json = $response->access_token;
return $access_token_json;
};

Related

Api Oppwa payment button, Problems with a foreach

I am integrating a Payment Button with Oppwa API,
But with the method that brings me the "CheckoutId".
What happens I have to send all the parameters.
$total = 120;
$iva = 004012000000000012;
$totalTarifa12 = 053012000000000100;
$totalBase0 = 052012000000000200;
$email = "JonathanVeraMarin98#outlook.com";
$primer_nombre = "Jonathan";
$segundo_nombre = "Fernando";
$apellido = "Vera";
$cedula = "0952152525";
$trx = 123456789;
$ip_address = '05100817913101';
$finger = 'https://test.oppwa.com/v1/checkouts';
$merchterm= '1000000505_PD100406';
$telefono ='2172161';
$direccion_cliente = 11222;
$pais_cliente = 121212;
$direccion_entrega = '1600 Pennsylvania Ave NW';
$pais_entrega = 121212;
$checkoutId = prepareCheckout($items,$total,$iva,$totalTarifa12,$totalBase0,$email,$primer_nombre,$segundo_nombre,$apellido,$cedula,$trx,$ip_address,$finger,$merchterm,$telefono,$direccion_cliente,$pais_cliente,$direccion_entrega,$pais_entrega);
These are the values ​​of the burned parameters that I sent to the function
function prepareCheckout($items,$total,$iva,$totalTarifa12,$totalBase0,$email,$primer_nombre
,$segundo_nombre,$apellido,$cedula,$trx,$ip_address,$finger,$merchterm,
$telefono,$direccion_cliente,$pais_cliente,$direccion_entrega,$pais_entrega){
$finger = urlencode($finger);
$i =0;
$url = "https://test.oppwa.com/v1/checkouts";
$iva = str_replace('.', '', $iva);
$totalTarifa12 = str_replace('.', '', $totalTarifa12);
$totalBase0 = str_replace('.', '', $totalBase0);
$valueIva = str_pad($iva,12,'0'.STR_PAD_LEFT);
$valueTotalIva = str_pad($totalTarifa12,12,'0'.STR_PAD_LEFT);
$valueIvaTotalBase0 = str_pad($totalBase0,12,'0'.STR_PAD_LEFT);
$data = "authentication.userId=8a8294184b4f2868014b4f86f767015d" .
"&authentication.password=F8T7N4PD" .
"&authentication.entityId=8a8294184b4f2868014b4f87bf160173" .
"&amount=".$total.
"&currency=USD".
"&paymentType=DB".
"&customer.givenName=".$primer_nombre.
"&customer.middleName=".$segundo_nombre.
"&customer.surname=".$apellido.
"&customer.ip=".$ip_address.
"&customer.merchantCustomerId=00000000001".
"&merchantTransactionId=transaction_".$trx.
"&customer.email=".$email.
"&customer.identificationDocType=IDCAD".
"&customer.identificationDocId=".$cedula.
"&customer.phone=".$telefono.
"&billing.street1=".$direccion_cliente.
"&billing.country=".$pais_cliente.
"&shipping.street1=".$direccion_entrega.
"&shipping.country=".$pais_entrega.
"&risk.paramters[USER_DATA2]=MiComercio".
"customParamters[".$merchterm."]=00810030070103910004012".$valueIva.
"05100817913101052012".$valueIvaTotalBase0."053012".$valueTotalIva;
foreach ($items["cart"] as $c )
{
$data.="&cart.items[".$i."].name =".$c["product_name"];
$data.="&cart.items[".$i."].description ="."Descripcion: ".$c["product_name"];
$data.="&cart.items[".$i."].price =".$c["product_price"];
$data.="&cart.items[".$i."].quantity= ".$c["q"];
}
$data.= "&testMode=EXTERNAL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Bearer OGE4Mjk0MTg1MzNjZjMxZDAxNTMzZDA2ZmQwNDA3NDh8WHQ3RjIyUUVOWA=='));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// this should be set to true inproduction
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responseData = curl_exec($ch);
if(curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $responseData;
}
My function that generates a checkoutId.
The problem I have is in the foreach I know that it is going through an array but I get an error because I don't know what the condition $ items ['cart'] refers to
foreach ($items["cart"] as $c )
{
$data.="&cart.items[".$i."].name =".$c["product_name"];
$data.="&cart.items[".$i."].description ="."Descripcion: ".$c["product_name"];
$data.="&cart.items[".$i."].price =".$c["product_price"];
$data.="&cart.items[".$i."].quantity= ".$c["q"];
}
I've tried it like this
$items = array();
$items['product_name'] = 'Software';
$items['product_name'] = 'Hola';
$items['product_price'] = '50';
$items['q'] = '1';
I hurt:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Datafast\index.php on line 63
`

Read JSON with php from instagram __a=1

original
I want to emphasize first that it is my first script in PHP, so many things can be improved, but for now I just need it to work!
I created this script in php to get public profile information from the public instagram json file located at https://www.instagram.com/{{username}}/?__a=1
trying it locally, everything works correctly, but hosting it on a website file_get_contents($ url) doesn't work (line 29) , I tried to use CURL to read the file, but it doesn't work anyway, it doesn't read the json file correctly, trying to do an echo of what he reads the instagram logo appears on the site screen.
how can I solve it?
update
I just noticed that if I try to make file_get_contents () of a link of any profile www.instagram.com/USERNAME, it gives me the exact same result, it may be that trying to read www.instagram.com/USERNAME/?__a= 1 instagram notice and redirect me to the profile page?
I've tried htmlentities() on the data I receive through file_get_contents ... tatan .. actually the script reads a strange html page that is NOT found at the address I gave it!
<?php
$commentiPost;
$likePost;
$postData;
$image;
$urlprofilo;
$followers;
$username;
$follow;
$like;
$commenti;
function getMediaByUsername($count) {
global $image;
global $commentiPost;
global $likePost;
global $urlprofilo;
global $followers;
global $username;
global $follow;
global $postData;
global $like;
global $commenti;
$uname = htmlspecialchars($_GET["name"]);
$username = strtolower(str_replace(' ','_',$uname));
$url = "https://www.instagram.com/".$username."/?__a=1";
$userinfo = file_get_contents($url);
$userdata = json_decode($userinfo,true);
$user = $userdata['graphql']['user'];
$iteration_url = $url;
if(!empty($user)){
$followers = $user['edge_followed_by']['count'];
$follow = $user['edge_follow']['count'];
$fullname = $user['full_name'];
$username = $user['username'];
$profilepic = $user['profile_pic_url'];
$profilepic = (explode("/",$profilepic));
$urlprofilo = "https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/s150x150/$profilepic[6]";
$limit = $count;
$tryNext = true;
$found = 0;
while ($tryNext) {
$tryNext = false;
$remote = file_get_contents( $iteration_url );
$response = $remote;
if ($response === false) {
return false;
}
$data = json_decode($response, true);
if ( $data === null) {
return false;
}
$media = $data['graphql']['user']['edge_owner_to_timeline_media'];
foreach ( $media['edges'] as $index => $node ) {
if ( $found + $index < $limit ) {
if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
$type = 'video';
} else {
$type = 'image';
}
$like = $like + $node['node']['edge_liked_by']['count'];
$commenti = $commenti + $node['node']['edge_media_to_comment']['count'];
$image[] = array( "<a href=".$node['node']['display_url'].">
<img src=".$node['node']['display_url']." alt="." />
<h3>Like: </strong>".$node['node']['edge_liked_by']['count']."</strong> Commenti: <strong>".$node['node']['edge_media_to_comment']['count']."</strong></h3>
</a>");
$postData[] = array(" '".gmdate("d-m-Y",$node['node']['taken_at_timestamp'])."',");
$likePost[] = array(" ".$node['node']['edge_liked_by']['count'].",");
$commentiPost[] = array(" ".$node['node']['edge_media_to_comment']['count'].",");
}
}
$found += count($media['edges']);
if ( $media['page_info']['has_next_page'] && $found < $limit ) {
$iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
$tryNext = true;
}
}
} else{
}
}
getMediaByUsername( 12);
if(isset($image))
{
$postTot = count($image);
}
else {
$postTot = 0;
}
if($postTot > 0 and $followers > 0){
$ER = round(((($like + $commenti)/$postTot)/$followers)*100, 1);
}
else {
$ER = 0;
}
?>
I belive that is SSL certificate problem. When you modify your function to:
function url_get_contents ( $url ) {
if ( ! function_exists( 'curl_init' ) ){
die( 'The cURL library is not installed.' );
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec( $ch );
if(curl_errno( $ch )) {
die ('Curl error: ' . curl_error($ch));
}
curl_close( $ch );
return $output;
}
Probably you will see as a result: Curl error: SSL certificate problem: unable to get local issuer certificate.
Add that certificate to your system or uncomment lines with options: CURLOPT_SSL_VERIFYHOSTand CURLOPT_SSL_VERIFYHOST.

Failing to get yahoo access token

Recently, I'm developing my site with social networking, and want to import gmail,yahoo,hotmail etc.. contacts.
I have already grabbed contacts from Gmail and now next want to do the same with Yahoo. Reading the Yahoo documentation and following my gmail code, I get the authorisation screen and get the auth code back, but unable to get the access token. (I am using curl and I am expecting help related to it).
Here is the dump: string
'code=hzbp3px&client_id=dj0yJmk9WlJ0a3dmQTQ3ZFcyJmQ9WVdrOVFXRXliSFpMTm1zbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1jZA--&client_secret=5b3a803684538bae27519af3e34f3e268da432a1&redirect_uri=http://localhost/DEVELOPMENT/Grabber.php&grant_type=authorization_code' (length=254)
object(stdClass)[5] public 'error' => string 'invalid_request'
(length=15)
Here is my functions:
public function get_url(){
global $session;
switch ($session->get_provider()) {
case "gmail": return "https://accounts.google.com/o/oauth2/token";
break;
case "yahoo": return "https://api.login.yahoo.com/oauth2/get_token";
break;
}
}
public function curl_query($post,$boolean){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$this->get_url());
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
var_dump($response);
//exit;
switch ($boolean) {
case "true": return $response->access_token;
case "false": return $response->refresh_token;
}
}
public function get_access_token($auth_code){
global $session;
switch ($session->get_provider()) {
case "gmail": return $this->curl_query($this->post_params($auth_code,'access_token'),true);
case "yahoo":
$accesstoken = $this->curl_query($this->post_params($auth_code,'access_token'),true);
//$refreshtoken = $this->curl_query($this->post_params($accesstoken),false);
return $accesstoken;
}
}
public function post_params($auth_code,$token){
global $session;
if($token == 'access_token'){
$fields=array(
'code'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
} else if($token == 'refresh_token'){
$fields=array(
'refresh_token'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
}
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
var_dump($post);
return $post;
}
I found out how to get the tokens, here is the code if anyone needs it
Anyway oauth2 is not working with yahoo as i experienced, bellow i am using oauth1.0
public function get_gmail_access_token($auth_code){
global $session;
$fields=array(
'code'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$url = 'https://accounts.google.com/o/oauth2/token';
$result = $this->curl_query_post($url,$post);
$response = json_decode($result);
return $response->access_token;
}
public function get_yahoo_access_token($token,$verifier){
global $session;
$tokens = array();
$fields=array(
'oauth_version'=> '1.0',
'oauth_nonce'=> rand(),
'oauth_consumer_key'=> $session->get_client_id(),
'oauth_signature'=> $session->get_client_secret().'%26'.$session->get_token_secret(),
'oauth_timestamp'=> time()+3600,
'oauth_signature_method'=> 'plaintext',
'oauth_token'=> $token,
'oauth_verifier'=> $verifier,
'oauth_callback'=> $session->get_redirect_uri()
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$url = 'https://api.login.yahoo.com/oauth/v2/get_token';
$result = $this->curl_query_post($url,$post);
$string = parse_str($result);
$tokens['oauth_token'] = $oauth_token;
$tokens['oauth_token_secret'] = $oauth_token_secret;
//$xoauth_yahoo_guid;
return $tokens;
}

Wordpress Forms

I was wondering if it is at all possible to write a php file that will take a form submission from an external server and add the submission to wordpress as a blog post. The file should return a post->ID. Thanks
If you want to have the form processed on the remote server, you can remotely add the submission to WordPress via XMLRPC.
An example as follows:
<?php
class XMLRPClientWordPress
{
var $XMLRPCURL = "";
var $UserName = "";
var $PassWord = "";
// constructor
public function __construct($xmlrpcurl, $username, $password)
{
$this->XMLRPCURL = $xmlrpcurl;
$this->UserName = $username;
$this->PassWord = $password;
}
function send_request($requestname, $params)
{
$request = xmlrpc_encode_request($requestname, $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
function create_post($title,$body,$category,$keywords='',$encoding='UTF-8')
{
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category)
);
$params = array(0,$this->UserName,$this->PassWord,$content,true);
return $this->send_request('metaWeblog.newPost',$params);
}
function create_page($title,$body,$encoding='UTF-8')
{
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body
);
$params = array(0,$this->UserName,$this->PassWord,$content,true);
return $this->send_request('wp.newPage',$params);
}
function display_authors()
{
$params = array(0,$this->UserName,$this->PassWord);
return $this->send_request('wp.getAuthors',$params);
}
function sayHello()
{
$params = array();
return $this->send_request('demo.sayHello',$params);
}
}
if(trim($_POST['subject'])=='' || trim($_POST['content'])==''){
die('All fields are required.');
}
$xmlrpc = new XMLRPClientWordPress("http://yoursite.com/xmlrpc.php" , "username" , "password");
$xmlrpc->create_post( mb_convert_encoding(stripslashes($_POST['subject']), 'HTML-ENTITIES', 'UTF-8'), mb_convert_encoding(htmlentities(stripslashes($_POST['content']), ENT_COMPAT, 'UTF-8'), 'HTML-ENTITIES', 'UTF-8'), 1);
echo 'Post successful!';
?>

Wordpress update_post_field not working

Can't get the fields to update with values i'm providing... what's wrong? Custom fields all all created and I see them... just not getting inserted. Using Types plugin to create fields, that's why there's a wpcf prefix.
function get_data($url) {
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function get_cords($input) {
$url = 'http://geocoder.ca/?locate='.urlencode($input).'&geoit=xml';
$get = get_data($url);
$result = xml2array($get);
return $result;
}
function set_ll() {
global $post;
$post_id = $post->ID;
if ($post->post_type == 'dealer') {
$dealer = array();
$dealer['city'] = get_post_meta($post->ID, 'wpcf-city', true);
$dealer['pro'] = get_post_meta($post->ID, 'wpcf-pro', true);
$dealer['pc'] = get_post_meta($post->ID, 'wpcf-pc', true);
$dealer['address'] = get_post_meta($post->ID, 'wpcf-address', true);
$dealer['full'] = $dealer['address'] . ', ' . $dealer['city'] . ', ' . $dealer['pc'] . ', ' . $dealer['pc'];
$dealer['longlatt'] = get_cords($dealer['full']);
if (update_post_meta($post_id, 'wpcf-longitude', $dealer['longlatt']['geodata']['longt'])) {
update_post_meta($post_id, 'wpcf-latitude', $dealer['longlatt']['geodata']['longt']);
}
}
}
add_action('save_post', 'set_ll');
as an assumption from quickly looking at the API docs, try using $post_id as your function's first parameter instead of using global $post
function set_ll($post_id) {
// global $post;
// $post_id = $post->ID;
$post = get_post($post_id);

Categories