Obviously I am not using the right keywords in my searches or am not understanding what others have written in blogs or forums, et al.
Looking for information on passing data between two servers. The data will be preferably contained within a JSON array.
If you know of someone who has written a blog fully encompassing this I would really like a chance to read it. Otherwise could you offer some thoughts?
More detailed:
As a user visits a page a PHP function will be called and some data will be "packaged up" in a JSON array and then a POST command to the other server. The second server after receiving the POST will do some processing and then "package up" some data and return it in a JSON array. Then the user will be presented with the results.
With the following I am receiving a HTTP 200 response. Just no data.
SITE 1:
$data_string = json_encode(array('user_id'=>123));
$ch = curl_init('http://site2.dev/retrieve');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$result = curl_exec($ch);
$status = curl_getinfo($ch);
curl_close($ch);
$result = json_decode($result);
SITE 2:
public function retrieve() {
return json_encode(array('some'=>'456'));
}
The Json array was chosen as it can be encrypted and yes HTTPS will be used in the final environment.
Both servers will have Laravel 4 as the PHP Framework.
Thank you for your thoughts and comments.
Add this:
curl_setopt($ch, CURLOPT_HEADER, 0);
and put quotes on content-length:
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json','"Content-Length: ' . strlen($data_string) . '"'));
Related
I try to get odata produced by a navision webservice. When I directly access the url given using chrome browser, the page asks for user name and password and then chrome shows xml data as expected.
But when i use a PHP script, it always returns "1".
My code looks like this:
$url = 'http://103.7.1.182:14048/DynamicsNAV71-6/OData/Company(\'Unit%20G%205\')/Item_Master_on_hand_no_Desc';
$login = 'Gem-gae\senzo1:Bsbsenzo2018';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/x-www-form-urlencoded',
'Content-Length: ' . strlen(1))
);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Is there any mistakes with that code ?
Thanks for your help
There's no mistake on your code, it successfully connects to the external server via cURL and retrieves the data.
The value in $output (if you do a var_dump) is true (that's why you see 1 if you echo it).
Said that, the issue is on the server you are contacting (103.7.1.182:14048).
You probably need to send some data or something, I can't tell you exactly what because I don't know what is in that server.
To send data, you can add a curl_setopt line:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Where $data is an array.
I am new at programming Slash commands in Slack. For one of my commands, I have a username and need to retrieve the user icon URL. I am using PHP to code them.
I was planning on using users.profile.get, since the tutorial here shows that one of the fields returned is the user icon URL.
However, I am trying to find examples on how to make a call to this method and have not found any. Could anybody give me a quick example of the call, including how to send the parameters?
This is how far I got:
$slack_profile_url = "https://slack.com/api/users.profile.get";
$fields = urlencode($data);
$slack_call = curl_init($slack_profile_url);
curl_setopt($slack_call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($slack_call, CURLOPT_POSTFIELDS, $fields);
curl_setopt($slack_call, CURLOPT_CRLF, true);
curl_setopt($slack_call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($slack_call, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: " . strlen($fields))
);
$profile = curl_exec($slack_call);
curl_close($slack_call);
I basically have $token and $user_name and need to get the profile picture URL. How do I format $token and $username as $data? Is the call correct?
If anybody recommends doing this a different way, I would appreciate any advice as well.
Thank you so much!
To get data into the right format to post to Slack is pretty straight forward. There's two options (POST body or application/x-www-form-urlencoded).
The query string for application/x-www-form-urlencoded is formatted like a get URL string.
https://slack.com/api/users.profile.get?token={token}&user={user}
// Optionally you can add pretty=1 to make it more readable
https://slack.com/api/users.profile.get?token={token}&user={user}&pretty=1
Just request that URL and you will retrieve the data.
The POST body format will use a similar code to what you have above.
$loc = "https://slack.com/api/users.profile.get";
$POST['token'] = "{token}";
$POST['user'] = "{user}";
$ch = curl_init($loc);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($error = curl_errno($ch)) { echo $error; }
//close connection
curl_close($ch);
echo $result;
I am relatively new to PHP and the wonderful world of API's. To set the scenario for you, we have 12 online stores all on different domains, some on different servers, all localised to local languages etc. All of them are running Wordpress with Woocommerce 2.3.13.
I am currently trying to make a 'web app' type of dashboard that calls Woocommerce data via API and displays it in a friendly manner from all of the stores. I am using PHP/HTML to display the data, I have so far managed to get the orders count for each site without issue. However now I am looking to delve a little more in to the reporting and get some sales figures and various other bits.
To achieve the order count display I have used the following code:
<?php
$data = array();
$data_string = json_encode($data);
$username = 'ck_111111111111'; // Add your own Consumer Key here
$password = 'cs_111111111111'; // Add your own Consumer Secret here
$ch = curl_init('https://example.com/wc-api/v2/orders/count/?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result,true);
echo $result['count'];
curl_close($ch);
?>
This displays the order count for that particular woocommerce store. No problem.
But now I would really like to get sales reports, I have tried adding in the date filters on the url, but when putting the date filter on the end of url like this
$ch = curl_init('https://tinywaist.co.uk/wc-api/v2/orders/count/sales?filter[date_min]=2015-01-18&filter[date_max]=2015-01-21?consumer_key='.$username.'&consumer_secret='.$password);
I don't get anything back at all.
I have been trying various manipulations on the above code to achieve what I need, I have searched high and low to find a solution to this. What I am really not grasping is how to call specific data from the Woocommerce API and display it on the front end as an array, orders count is as far as I have got.
I have read, re read and re read again the Woocommerce REST API docs, but just cannot seem understand how to apply what I am reading in to a curl command within the PHP.
If you need any more info to help please just ask, I am bald, but if I had hair, I would be tearing it out!
Thanks in advance!
From this tutorial it appears that date_min and date_max are not the correct filters. The tutorial says this is the correct approach for getting the orders in January 2014.
$ curl
https://www.skyverge.com/wc-api/v1/orders?filter[created_at_min]=2014-01-01&filter[created_at_max]=2014-01-31
For anyone looking for the answer to my question I will repost the CURL request I am using.
The documentation on Woocommerce REST API is pretty poor and lacking in quantity!
$data = array();
$data_string = json_encode($data);
$username = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init('https://example.com/wc-api/v2/orders/?filter[period]=year&filter[status]=processing&consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
I am now having issues with parsing the JSON data coming out, but that is another story!
Please excuse my terminology if I get anything wrong, I'm new to Google API
I'm trying to add events via the Places API, for a single venue (listed under the bar category). I've followed the instructions here:
https://developers.google.com/places/documentation/actions?utm_source=welovemapsdevelopers&utm_campaign=places-events-screencast#event_intro
and this is the URL I am posting to (via PHP)
https://maps.googleapis.com/maps/api/place/event/add/format?sensor=false&key=AIzaSyAFd-ivmfNRDanJ40pWsgP1 (key altered)
which returns a 404 error. If I have understood correctly, I have set the sensor to false as I am not mobile, and created an API key in Google apis, with the PLACES service turned on.
Have I missed a vital step here, or would a subsequent error in the POST submission cause a 404 error? I can paste my code in but I thought I'd start with the basics.
Many thanks for your help, advice and time. It's very much apprecciated.
I've added this line to my CurlCall function which I believe should specify a POST, and the result is still the same.
curl_setopt($ch, CURLOPT_POST, 1);
so the whole functions reads
function CurlCall($url,$topost)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
$body = curl_exec($ch);
curl_close($ch);
echo $body;
echo '<br>';
echo 'URL ' . $url;
echo '<br>';
return $body;
}
Are you sure that you are using the POST method and not GET?
You need to specify the format of your request in the URL by changing the word format to either json or xml depending on how you are going to structure and POST your request.
XML:
https://maps.googleapis.com/maps/api/place/event/add/xml?sensor=false&key=your_api_key
JSON:
https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=your_api_key
This should be simple I think but I just can't fathom it.
I need to make a request to a url in the following format:
http://ratings.api.co.uk/business/{lang}/{fhrsid}/{format} where lang will be set to en-GB the fhrsid is an identifier and format is json.
I tried the following but I am just getting null in return:
$data = array("lang" => "en-GB", "fhrsid" => "80928");
$data_string = json_encode($data);
$ch = curl_init('http://ratings.api.co.uk/business/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)) );
$Jsonresult = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($Jsonresult));
Any help gratefully received
You are currently posting the data to the URL, while you say you want to put the data in the URL itself.
I.e. now you submit {"lang:"en-GB","fhrsid":80928} to the URL http://ratings.api.co.uk/business/json, but instead you want to retrieve the URL http://ratings.api.co.uk/business/en-GB/80928/json.
Don't use POST as request type, don't specify postfields, don't specify content-length, and do put the data in your URL.
Sending data as arguments is different than sending it within the URL.
If you require a url format of http://ratings.api.co.uk/{lang}/{fhrsid}/{format}, then you must make your curl_init string match that format.