I am very new to echonest and curl. I have an up and running echoprint server and echoprint codegen is also working fine. I am able to push codes to my local server using ingest method.
But I don't know how to query the server using the curl request.
How can I send the syntax or example of the curl request for querying the echoprint server?
Once you are up with code parsed by codegen library you can always call api layer for ingest, sample code where i use PHP script top of the echoprint server
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://localhost:8080/ingest',
CURLOPT_USERAGENT => 'URl Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'fp_code' => $songcode,
'track_id' => $trackid,
'length'=>$length,
'artist' => $artist,
'codever' => $codecover
)
) );
$resp = curl_exec( $curl );
curl_close( $curl );
Related
I am trying to write my first REST API with PHP. We want to send shipping data to the shipping company and get labels for printing in return. The shipping company has given me test api credentials.
So I started to write a plugin with this code inside:
$url = "https://mywebsite/endpoint/";
$username = "myusername";
$password = "mypassword";
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
)
);
var_dump($response); // not being called
When I run this code, I get a white screen with no error message, the last line with var_dump($response) is not being called. I am a bit stuck here because I se no success and no error...what could it be? Why this behaviour of the remote server? Does it mean "sorry, wrong credentials" or "data has the wrong format"? I have the feeling that the server doesn't even notice that I'm trying to contact him...
I tried a number of other variatons of the above code that I found somwehere online, and also outside of WordPress, but no success.
The shipping company gave me a documentation. It says:
HTTP Method: POST
Authentication Header: Basic Authentication (user name/ password)
so I thought I could do no wrong when trying that. Hm.
I found a solution, so I can share it here with you.
I installed the free Software Postman and created a simple POST query there. After entering $url, $username and $password I ran a query and it worked well. the good thing abort Postman is that it creates code snippets for you. "very_long_cryptic_string" is obviously created from $username and $password. The PHP for my case goes like this, and it works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic very_long_cryptic_string'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I have implemented the sigle signon in worpress with Java with Aws cognito.
it is working fine with http but after moving http to https. API's are not working and throwing the error below:
WP_Error Object
(
[errors] => Array
(
[http_request_failed] => Array
(
[0] => cURL error 35: error:1408F10B:SSL routines:ssl3_get_record:wrong version number
)
)
[error_data] => Array
(
)
)
I have consumed the get and Post API like below:
$bodyData = array(
'username' => $_POST['username'],
'password' => $_POST['password']
);
$response = wp_remote_post('https://example.com:8083/digitalIdentityProvider/login',
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '2.0',
'sslverify' => true,
'blocking' => true,
'headers' => array('Content-Type'=> 'application/json'),
'body' => json_encode($bodyData) ,
'cookies' => array()
));
Could you please someone guide or suggest ?
Thanks.
After playing with wordpress code and AWS, I found the issue with Aws ELB and Load Balancer .
SSL Certificate has applied to LB and Third party API and wordpress were hosted on AWS EC2 and for EC2 there was not SSL certificate and I was trying to access API with
HTTPS:
https://example.com:8083/digitalIdentityProvider/login hence request was not verifying over there hence I was not receiving any response from API.
I have changed API call
from https://example.com:8083/digitalIdentityProvider/login
to http://example.com:8083/digitalIdentityProvider/login
after change it https to http. it working fine.
I am trying to push notification from server to all android devices with the same topic calling Google firebase cloud messaging api,however, http response says 411 error code which stands for POST requests require a Content-length header, but I do add content-length in http post request header.I have googled the issue, and tried several ways, but it seems do not work.I use php version 7.0.I use postman to send the request, it was doing ok, even without header:content-type..
$topic = 'topic';
$projectId = 'projectid';
$title = 'hahaha';
$content = 'lol';
$payload = array(
'message' => array(
'topic' => $topic,
'notification' => array(
'title' => $title,
'body' => $content,
)
)
);
$json = json_encode(trim($payload));
$headers = array(
'Authorization:Bearer '.$this->getFcmApiAccessToken(),
'Content-Type: application/json; UTF-8',
'Content-length:'.strlen($json),//'Content-length:0'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://fcm.googleapis.com/v1/projects/{$projectId}/messages:send");
curl_setopt($ch,CURLOPT_POST, true );
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);exit;
I've just integrated the marketing tool Klaviyo with our Wordpress/WooCommerce set up and I'm trying to push user meta data through a cURL API - but failing!
You can find out how the API works here: https://www.klaviyo.com/docs/http-api#people
I'm hoping to add an action so when the user profile saves, it hooks in my function sending the meta data through to Klaviyo.
Can anyone see what I've done wrong please - code below?
Thanks so much in advance.
Linz
<?php
// Hook into the action which saves the User Meta Data (written by LD)
add_action( 'personal_options_update', 'klaviyo_send' );
add_action( 'edit_user_profile_update', 'klaviyo_send' );
function klaviyo_send () {
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'token' => 'haqXiq', //This is the public 'key' in Klaviyo
'$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
'twitter' => $user_id->twitter,
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
}
?>
Try sending data by this way .. change the posted data to url friendly ways like ?key=value&key=value&key=value
//extract data from the post
//set POST variables
$url = 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=';
$fields = array(
'token' => 'haqXiq', //This is the public 'key' in Klaviyo
'$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
'twitter' => $user_id->twitter,
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I'm using the Google Closure Compiler RESTFul API with WordPress.
The request is created using wp_remote_post() and so far everything went okay.
What I want to know is how to get the API to not only return the compiled code, but also the warnings, errors and statistics.
Supplying 'output_info' => array( 'compiled_code', 'warnings', 'errors', 'statistics' ) in the body parameter seems not to work and the API returns errors. Any ideas?
Thank you very much!
Just looked around and found out that Closure Compiler accepts output_info parameter multiple times. This is not possible with the WP_Http API without some modifications.
So I looked at the source of WP_Http and did the following, now it's working :)
// Default request data
$request_data = array(
'output_info' => array( 'compiled_code', 'warnings', 'errors', 'statistics' ),
'output_format' => 'json'
);
$request_data = array_merge( $request_data, $args, compact( 'js_code' ) );
// Process the request body manually to make same named parameters possible
$body = http_build_query( $request_data, null, '&' );
$body = preg_replace( '/output_info%5B\d+%5D=/', 'output_info=', $body );
// Initiate request
$response = wp_remote_post( CLOSURE_COMPILER_URL, array(
'sslverify' => false,
'timeout' => 10,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' )
),
'body' => $body
));