PHP Proxy POST request to API with file upload - php

I have a job application form that contains a bunch of text inputs and two fields to upload files (resume and cover letter).
This form needs to post to a job board API endpoint but the request has to be proxied so the API key isn't seen in the post. This is working fine with just the text inputs but I can't figure out how to get the files to upload as well. Below is what I have so far, which only posts the text.
How do I need to go about uploading the files as well?
<?php
if ( $_POST ) {
// API key
$api_key = 'super_secret_key';
$url = "https://api.myjobboard.io/applications/";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_USERPWD, $api_key . ":" . '' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $_POST ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$response = curl_exec( $ch );
echo $response;
} else {
echo '<p>Error: No post data.</p>';
}
?>

Related

Need help on Facebook API how to get data from array with multiple fields

$endpointFormat = ENDPOINT_BASE . '{page-id}?fields=about,bio,description,new_like_count,talking_about_count,category,fan_count,link,name,rating_count,website,whatsapp_number,followers_count,country_page_likes,were_here_count,location&access_token={access-token}';
$statsAccountEndpoint = ENDPOINT_BASE . $pageId;
$statParams = array(
array ('fields' => 'about','bio','description','new_like_count', 'talking_about_count','category', 'fan_count', 'link','name','rating_count','website','whatsapp_number','followers_count','country_page_likes','were_here_count','location'),
'access_token' => $singleRow['fbaccesstoken']
);
// add params to endpoint
$statsAccountEndpoint .= '?' . http_build_query( $statParams );
// setup curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $statsAccountEndpoint );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// make call and get response
$response = curl_exec( $ch );
curl_close( $ch );
$responseArray = json_decode( $response, true );
Printing out the statParams
Printing out responseArray which only shows the name and page id
Just wanting to get the stats of the page im linking, tried with only 1 field variable before and it showed fine. Thanks for your time!
$statParams = array(
'fields' => 'about,bio,description,new_like_count,talking_about_count,category,fan_count,link,name,rating_count,website,whatsapp_number,followers_count,country_page_likes,were_here_count,location',
);
just had to make it so that everything is in one big '' instead of separate.

How can I send order Id in the url and get response?

Here is my code And After execute this code i got this error "Response : {"Message":"No HTTP resource was found that matches the request URI 'http://example.com/Api/WooCommerceApi/SaveSubscriptionAndZoomData'.","MessageDetail":"No action was found on the controller 'WooCommerceApi' that matches the request."}"
function my_api_call( $order_id ){
// Order Setup Via WooCommerce
$order = new WC_Order( $order_id );
// Iterate Through Items
$items = $order->get_items();
$url = "http://example.com/Api/WooCommerceApi/SaveSubscriptionAndZoomData";
$orderid = "OrderId=".$order_id;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $orderid);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
write_log ("Order Id for new user: " .$orderid ." and Response is : ".$response);
}
add_action( 'woocommerce_payment_complete', 'my_api_call');
I hope You will understad may question. And I want to print Json
response also in the file

Inline keyboard Telegram bot [PHP]

I want that my bot when I write /orario he answer me with a inline keyboard.
So.. I created an array for my keyboard in this way:
$tastierino_giorno = '[{"text":"Testo","callback_data":"StampaMessaggio"}]';
and in another function I write this:
function tastieraInline($chatid, $tastierino)
{
global $token;
$messaggio = "Scegli per che giorno inviare il messaggio:";
$tastiera = '&reply_markup={"inline_keyboard":
['.urlencode($tastierino).'],"resize_keyboard":true}';
$url = "https://api.telegram.org/$token/sendMessage?chat_id=$chatId&parse_mode=HTML&text=".urlencode($messaggio).$tastiera;
file_get_contents($url);
}
After this, with an if, I check if the users has write "/orario".
} elseif($message == "/orario"){
tastieraInline($chatid, $tastierino_giorno);
}
Now the problem is that it doesn't works... what's the problem?
Change
$tastiera = '&reply_markup={"inline_keyboard":
['.urlencode($tastierino).'],"resize_keyboard":true}';
to
$tastiera = '&reply_markup='.urlencode('{"inline_keyboard":
['.$tastierino.'],"resize_keyboard":true}');
you should URL encode the whole JSON data
You should pay attention to the result returned from your telegram calls. file_get_contents is great for getting something working quickly but doesn't return any error information.
You should use curl or a library like guzzle. An example for curl:
$ch = curl_init( $url );
if ( $ch == FALSE )
{
error_log( "Error initialising curl" );
return FALSE;
}
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_POST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_FORBID_REUSE, 1 );
// Set TCP timeout to 30 seconds
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Connection: Close' ) );
$result = curl_exec( $ch );
$error = curl_errno( $ch );
$errorStr = curl_error( $ch );
curl_close( $ch );
if ( $error != 0 )
{
return array( $errorStr, $result );
}
return $result;
Guzzle is a lot simpler if you don't mind installing an additional library.
Hopefully this will get you the error string from your failed telegram calls.
Onto your actual issue. You should create json for the keyboard markup using json_encode rather than appending strings. http_build_query makes building URLs much easier.

Transfer array from one page to another using CURL

Im trying to transfer an array of data between two files.
The sender.php code (the file sending the array using POST method)
$url = 'http://localhost/receiver.php';
$myvars = array("one","two","three")
$post_elements = array('myvars'=>$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_elements);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo "$response";
The receiver.php code (The file receiving the array from sender.php file and then take each element of the array and echo it and also put it in a document saved.txt.
echo $_POST($myvars); // To test the output of the received data.
foreach($myvars as $item) {
if (!empty($item)) {
echo $item."<br>";
$myfile = file_put_contents('Saved.txt', (" Name: ". ($_POST["$item"])) . PHP_EOL , FILE_APPEND);
}
}
The array isn't being transferred to the receiver.php or I am not catching it. In the document output I have only in the place of the variable $item instead of each element of the array.
Edit:
Added the following code in the receiving file in order to get the array elements from inside but all I get is array printed out:
foreach( $_POST as $stuff ) {
if( is_array( $stuff ) ) {
foreach( $stuff as $thing ) {
echo $thing;
}
} else {
echo $stuff;
}
}
By adding on the receiving file the following:
echo "<pre>";
print_r($_POST);
echo "</pre>";
I get the following:
Array
(
[myvars] => Array
)
OK, the bottom line of the discussion in the comments above leads to this result:
The sending part:
<?php
$url = 'http://localhost/out.php';
$myvars = array("one","two","three");
$post_elements = array('myvars'=>$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($post_elements));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
print_r($response);
The receiving part:
<?php
print_r($_POST);
The output on the sending side is:
Array ( [myvars] => Array ( [0] => one [1] => two [2] => three ) )
which basically says that you can simply use $_POST['myvars'] on the receiving side which will exactly hold the scalar array you want to transfer.
try to serialize the array because it always helps me :
$url = 'http://localhost/receiver.php';
$myvars = array("one","two","three");
$myvars_post=join(" ",$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, "array=".urldecode($myvars_post));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo "$response";
and in the receiver.php use :
print_r($_POST);

Cleaning facebook url cache from php

I have an issue with the facebook linter feature. I'm trying to clean the Facebook cache of one my URL, with a PHP curl query. As you can see here (Updating Objects), Facebook got an api for scrapping all the content of an URL and update URL Facebook meta. It's the same API used in the Facebook Debugger Tool.
If I use their debugger manually, there is no problem. My URL is correctly scrapped and all corrects meta are retrieved. But it's not the case with a php curl query.
Here my actual code :
$furl = 'https://graph.facebook.com';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $furl );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
$params = array(
'id' => "http://www.gametrader-app.com/aTPVev",
'scrape' => true,
);
$data = http_build_query( $params );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
$resultLinter = curl_exec( $ch );
The API result displays incorrect/old facebook meta.
Moreover, I have tried to call directly the URL of the Debugger without success
curl_setopt( $ch , CURLOPT_URL, "http://developers.facebook.com/tools/debug/og/object?q=http://www.example.com");
curl_setopt( $ch , CURLOPT_HEADER , 0 );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt( $ch , CURLOPT_USERAGENT , $useragent );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$str_response = curl_exec( $ch );
I need to do this for one reason. Here the full process :
Publish a new ad on the iPhone App "GameTrader"
Post an open graph on facebook for this publish
That's all ;)
If I don't clean the URL cache on Facebook side before the post open graph, I get this error :
Error during open graph : OAuthException: (#3502) Object at URL
http://www.gametrader-app.com/aTPVev has og:type of 'website'. The property 'ad' requires an object of og:type 'gametrader-app:ad'
Can you help to understand what I'm doing wrong ?
Cheers!

Categories