I seek in stackoverflow a possible solution but any is good for me.
I will send the next json-rpc code to another file. This file contains a bottle server.
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method":"leer_datos", "params": {"bd":"escuela","tabla":"secretaria"}}' http://localhost:8081/rpc/alchemy
Now, i have in a file the next code for use cURL:
<?php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://localhost:8081/rpc/alchemy',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => "{'jsonrpc': '2.0','method':'leer_datos','params':{'bd':'escuela','tabla':'secretaria'}}"
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
I know the json is not well envoy.
How can i send the json to the url?
Sorry, my english is bad.
JSON data (key and value) must be only in "double quote".
{
"jsonrpc": "2.0",
"method": "leer_datos",
"params": {
"bd": "escuela",
"tabla": "secretaria"
}
}
Related
I am trying to connect our invoice service API to send e-invoices. I have API instructions, but I have no idea how to put all relevant fields to cURL body in the right way. I am using PHP form like this:
$curl = curl_init($url);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set the CURLOPT_INSECURE to disable certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Set the request data as JSON using json_encode function
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_INFILESIZE, $fileSize);
// Set custom headers for RapidAPI Auth and Content-Type header
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'x-api-mandator-uuid: mandator-api-id',
'x-api-key: mandator-api-key',
'Content-Type: application/json'
]);
// Execute cURL request with all previous settings
$response = curl_exec($curl);
// Close cURL session
curl_close($curl);
This is how body should be formatted:
curl --location --request POST 'https://api.address.here' \
--header 'Content-Type: application/json' \
--header 'x-api-key: API KEY HERE' \
--header 'x-api-mandator-uuid: MANDATOR ID HERE' \
--data-raw '{
"routingInstructions": {
"primaryDeliveryChannel": 3,
"sentUsingChannel": 0,
"eInvoiceNumber": "$customer-e-invoice address",
"eInvoiceOperator": "$eoperator"
},
"debtors": [
{
"debtorID": "$id",
"debtorType": 2,
"businessName": "$customername",
"businessID": "$businessid",
"businessOffice": "",
"personFirstName": "",
"personLastName": "",
"co": "",
"contactPerson": "",
"personSSN": "",
"postalAddress": {
"countryName": "Suomi",
"countryCode": "FI",
"streets": [
"$customer-street-addr"
],
"city": "$customer-city",
"zip": "$customer-zip"
},
"emails": [
"$customer-email"
],
"phoneNumbers": [
"$customer-phone"
]
}
],
"invoiceFile": {
"data": "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZ....",
"filename": "invoice - einlasku.xml"
},
"attachments": [],
"setAssignmentInStatus": 1
I have invoice file in xml-format and it should be Base64 coded.
Can somebody guide me how to put all this info to cURL body in correct way?
In the PHP Curl documentation, there is CURLOPT_POSTFIELDS which accepts key=>value array so in your case something like
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'routingInstructions' => [
'primaryDeliveryChannel' => 3
...
],
'debtors' => []
...
]);
However I can see you need to send file, which can be sent using CURLFile.
I am finding easier to use library such as Guzzle for advanced HTTP Post.
I'm using this service online-convert trying to make a simple call like in the online-convert sample. However they do not have examples of actual code so I'm kinda in the dark. online-convert docs
I have got that far, here is my best guess at what a call should look like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api2.online-convert.com/jobs");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: https://api2.online-convert.com",
"X-Oc-Api-Key: <my api key>",
"Content-Type: application/json"
));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
Any help will be appreciated.
They are using a REST API to convert files. Here is an example.
First create a json file with a link where the file you want to convert can be downloaded (uploads are handled differently). Also add the format you want to convert to. Save it as test.json.
{
"input": [{
"type": "remote",
"source": "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png"
}],
"conversion": [{
"category": "image",
"target": "png"
}]
}
Then send this file using curl to the API of online-convert.com. Add your API key to the script below and save it as start.php in the same directory where you saved the test.json:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api2.online-convert.com/jobs",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => file_get_contents('test.json'),
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"x-oc-api-key: <your API key here>"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Execute the PHP file on the command line with
php save.php
You can also call the script using a webbrowser.
After you have successfully sent the job and got a valid response, you can obtain the status of the conversion. For this you need the id (job id) you got in the answer when executing start.php. Create a file called status.php and execute it.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api2.online-convert.com/jobs/<your job id here>",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"x-oc-api-key: <your API key here>
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
There you will find the URL to download your file.
The API is much more powerful than that. You can upload files you want to convert, create multiple conversions of one single file (e.g. videos in different resolution with one API call) and set various conversion options.
I'm sending the following request to Google Analytics:
http://www.google-analytics.com/collect?v=1&tid=UA-72579327-1&cid=61baecac-8f8c-4dce-bf07-a7efa24a4e47&t=transaction&ti=qcY6pvpWGmP9fHyi&tr=10.00&cd1=Acme Racing&cd2=http://www.domain.co.uk/affiliate/idevaffiliate.php/id=314&tid1=12044762460674420322&url=http://www.nitrotek.co.uk&cd3=A1&cd4=Upgrades&cd5=Acme-Tech &cd6={device}&cd7=&cd8=0&cd9=&cd10=&cd11=&cd12=Nitrotek DSA&cd13=g&cd14=50&cd15=1&cd16=85&cd17=12044762460674420322&cd18=&cd19=http://www.domain.co.uk&cd20=61baecac-8f8c-4dce-bf07-a7efa24a4e47&gclid=
When you go to Google's hit builder and validate the request, it comes out valid.
However, when I send the same request through CURL POST, I just get "400. That’s an error. Your client has issued a malformed or illegal request. That’s all we know.".
POST is the correct method for this request (though I have tried GET just in case) and the Content-Length header is the length of the sent data (string). Here is the code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $transaction_url, //the same URL string given above
CURLOPT_POST => 1,
CURLOPT_USERAGENT => 'cURL Request'
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Length: '.strlen($transaction_url) //length of URL string
));
$trans_resp = curl_exec($curl);
var_dump($trans_resp);
curl_close($curl);
Hello: I am trying to do an API call using curl_init; have made some good progress but seem to be stuck...
we have an interface (swagger) that allows us to do the curl call and test it which works: here is the curl commands from that:
curl -X POST --header "Content-Type: application/x-www-form-urlencoded"
--header "Accept: application/json"
--header "Authorization: Bearer xxxxx-xxxxxx- xxxxx-xxxxxxx"
-d "username=xxxxxxxx39%40gmail.com&password=xxxx1234" "http://xxxxxxxxx-xx-xx-201-115.compute-1.amazonaws.com:xxxx/api/users"
here is my attempt to do the same call in PHP code:
$json = '{
"username": "xmanxxxxxx%40gmail.com",
"password": "xxxx1234"
}';
$gtoken='xxxxxx-xxxxxx-xxx-xxxxxxxx';
$token_string="Authorization: Bearer ".$gtoken;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://exx-ccc-vvv-vvvv.compute-1.amazonaws.com:xxxx/api/users', //URL to the API
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HEADER => true, // Instead of the "-i" flag
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded','Accept: application/json',$token_string)
));
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
$resp = curl_exec($curl);
curl_close($curl);
I am getting a response code "500" which makes me think that there is something wrong with my input. So I am wondering if anyone can help with this...
In your command line code, you post a standard URL encoded data string using -d "username=xxxxxxxx39%40gmail.com&password=xxxx1234" but in PHP you are creating a JSON string and sending it as a single post field (not properly URL encoded).
I think this is what you need:
$data = array(
'username' => 'xmanxxxxxx#gmail.com',
'password' => 'xxxx1234',
);
$data = http_build_query($data); // convert array to urlencoded string
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
As far as I can tell the rest of the code looks fine.
Also, you don't explicitly need to set the Content-Type header, cURL will do this for you when you pass a string to CURLOPT_POSTFIELDS. It will set it to multipart/form-data if you pass an array to CURLOPT_POSTFIELDS. But having it doesn't hurt anything either.
I am using php and I want to create a HTTP request to access some API data. I have a document that says, I need to place the following request
GET /abc/api/Payment HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-PSK: [App Key]
X-Stamp: [UTC Timestamp]
X-Signature: [HMACSHA256 base 64 string]
Body:
var1, var1
I have app key, I can get UTC Timestamp and I can create signature. I am not sure how to start creating this request? I am using codeingiter. If someone can help with example to set the header and body?
I also tried this url https://www.hurl.it/ to place requests but can't make it work. Any suggestions?
You want to use cURL's CURLOPT_HTTPHEADER option.
http://php.net/manual/en/function.curl-setopt.php
This should get you started
function request($url) {
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
"X-PSK: [App Key]",
"X-Stamp: [UTC Timestamp]",
"X-Signature: [HMACSHA256 base 64 string]"
),
CURLOPT_FOLLOWLOCATION => true
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if (curl_error($ch)) die(curl_error($ch));
curl_close($ch);
return $answer;
}