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);
Related
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.
"¤cy=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
`
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.
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;
};
I am searching products on an e-commerce website using their API. The API returns all the information in JSON format as a response. Each JSON response has 50 items and a URL that has 50 other items and so on. So to match the searched string with product titles, I need to parse all the JSON files sequentially. But if the item to be searched is on the last page, it is taking about 40 minutes to reach there. Can you please help on how I can reduce that time?
My website is currently hosted on localhost(XAMPP).
index.php
$fk = new Flipkart();
$fk->getProductFeedJason($fk->result);
for($i=0; $i<51; $i++)
{
if($fk->flag==1)
break;
$fk->curl($fk->links[$i]);
$fk->getProducts($fk->result, $sstring);
}
flipkart.php
<?php
class Flipkart
{
private $baseUrl = "https://affiliate-api.flipkart.net/affiliate/api/psblesson.json";
private $headers = array(
'Fk-Affiliate-Id: id',
'Fk-Affiliate-Token: token'
);
public $result;
public $links;
public $mainLinksCount=0;
public $pc=0;
public $id;
public $title;
public $image;
public $sellingPrice;
public $maximumRetailPrice;
public $productURL;
public $flag=0;
function __construct()
{
ini_set('max_execution_time', 0);
ini_set('memory_limit', '1024M');
$this->curl($this->baseUrl);
}
public function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$this->result = curl_exec($ch);
curl_close($ch);
}
public function getProductFeedJason($res)
{
$i=0;
$productFeeds = json_decode($res, TRUE);
$this->links = $productFeeds['apiGroups']['affiliate']['apiListings'];
foreach($this->links as $keys=>$value)
{
$this->links[$i] = $value['availableVariants']['v0.1.0']['get'];
//echo $this->links[$i];
$i = $i + 1;
}
}
public function getProducts($res, $str)
{
$products = null;
$products = json_decode($res, TRUE);
$outerPart = $products['productInfoList'];
$nextUrl = $products['nextUrl'];
//echo $nextUrl.'<br />';
foreach($outerPart as $data)
{
$t = $data['productBaseInfo']['productAttributes']['title'];
similar_text($str, $t, $percent);
if($percent>70)
{
$this->id[$this->pc] = $data['productBaseInfo']['productIdentifier'] ['productId'];
//echo $this->id[$this->pc].'<br />';
$this->title[$this->pc] = $data['productBaseInfo']['productAttributes']['title'];
//echo $this->title[$this->pc].'<br />';
$this->image[$this->pc] = $data['productBaseInfo']['productAttributes']['imageUrls']['400x400'];
//echo $this->image[$this->pc].'<br />';
$this->sellingPrice[$this->pc] = $data['productBaseInfo']['productAttributes']['sellingPrice']['amount'];
//echo $this->sellingPrice[$this->pc].'<br />';
$this->maximumRetailPrice[$this->pc] = $data['productBaseInfo']['productAttributes']['maximumRetailPrice']['amount'];
//echo $this->maximumRetailPrice[$this->pc].'<br />';
$this->productUrl[$this->pc] = $data['productBaseInfo']['productAttributes']['productUrl'];
//echo $this->productUrl[$this->pc].'<br />';
$this->pc = $this->pc+1;
if($this->pc >=10)
{
$this->flag=1;
break;
}
}
}
if($nextUrl && $this->flag==0)
{
$this->curl($nextUrl);
$this->getProducts($this->result, $str);
}
}
}
?>
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!';
?>