I need to do this command
root#debian:~# curl -X PUT -d '{ "date": "2.5.", "order": 2, "prize": 45 }' '[URL]'
in PHP (or Python). But I have no idea how to do it. I tried this:
$data = '{ "date": "2.5.", "order": 2, "prize": 45 }';
$data = json_encode($data);
echo $data;
$ch = curl_init([URL]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
But this returns:
{ "error" : "Error: No data supplied." }
Any idea how to reproduce it in PHP/Python?
$url = "[URL]";
$data = array(
"date" => "2.5.",
"order" => "2",
"prize" => 45
);
$json_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
subprocess.call or subprocess.run will do what you need for this, although the output will get dumped to stdout, so you'll also need to redirect it if you want to manipulate the returned data. However, you could also use requests, like some other commenters have suggested.
https://docs.python.org/3.5/library/subprocess.html
import subprocess
import tempfile
with tempfile.TemporaryFile() as tmp:
subprocess.call(["curl", "-X", "PUT", "-d", '{ "date": "2.5.", "order": 2, "prize": 45 }', '[URL]'], stdout=tmp)
tmp.seek(0)
data = tmp.read()
Accoridng to the PHP docs,
http://php.net/manual/en/function.http-build-query.php
http_build_query accepts an array or object as the paramater.
Also, json_encode returns a string:
http://php.net/manual/en/function.json-encode.php
So, you need to change the way you are using them.
Further, using http_build_query might be preferable since it url-encodes the given params:
When POSTing an associative array with cURL, do I need to urlencode the contents first?
So you need to pass an array to the http_build_query function to make it work:
Example (from above link):
$curl_parameters = array(
'param1' => $param1,
'param2' => $param2
);
$curl_options = array(
CURLOPT_URL => "http://localhost/service",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query( $curl_parameters ),
CURLOPT_HTTP_VERSION => 1.0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
);
$curl = curl_init();
curl_setopt_array( $curl, $curl_options );
$result = curl_exec( $curl );
curl_close( $curl );
Related
I would like to verify whether someone has a valid ISIC card, I have written the following code for this Rest API (http://nakoduj.to/_upload/project_files/2015-08-18-12-51-20_DM%20-%20Integration%20Manual.pdf), but it doesn't work, and I have no idea why it doesn't.
$data = array( "cardNumber" => "S123456789000A",
"cardholderName" => "John Doe");
$data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://gts-dm.orchitech.net/api/verifications');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "testdm:testdm");
$result = curl_exec($ch);
if ($result === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
I'm getting false for the $result always. And there is no additional information in $info.
Thank you in advance for your help
<?php
$data = [
"cardNumber" => "S123456789000A",
"cardholderName" => "John Doe",
];
$data = json_encode($data);
$header = $request_headers = [
"Content-Type: application/json"
];
$curl_options = [
CURLOPT_URL =>'https://gts-dm.orchitech.net/api/verifications',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_AUTOREFERER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FILETIME => true,
CURLOPT_FRESH_CONNECT => true,
CURLOPT_USERPWD => "testdm:testdm"
];
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
The request header was not sent causing http error code 415 UnsupportedMediaTypeHttpException Hence need to add request header, working fine
I recommend you to use Postman. Just install it and try to make request with it. You'll see the result of your request and than can simply get the request code out.
You can see 'Code' on your top-right corner below 'save', press it and select your language. For your purposes it is PHP->cURL. And the result will look like this:
More about Postman here
So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.
I have an API which says all requests are made as POST requests with data in XML format, and it gives me a sample XML data:
<?xml version="1.0" ?>
<Request>
<SystemName>SomeSystemName</SystemName>
<Client>SomeClientID</Client>
<Method action="SomeAction">MethodParams</Method>
</Request>
Now I use curl to do it, like in this function:
function curl_post_xml($url, $xml, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $xml,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
And in $xml I put that XML string from above. Am I doing it right? Because I heard that when using POST, the data must be in key-value format, but the API doesn't say anything about which variable should I assign XML string to.
Am I doing it right? Because I heard that when using POST, the data must be in key-value format,
Yes, you are right, you have to specify that you are sending an XML that's all.
CURLOPT_HTTPHEADER => array("Content-Type: application/xml"),
// or text/xml instead of application/xml
You do not need to put the $xml under a key. Just pass it as you are doing, its fine.
This works for me:
<?php
$xml_data ='<Request>
<SystemName>SomeSystemName</SystemName>
<Client>SomeClientID</Client>
<Method action="SomeAction">MethodParams</Method>
</Request>';
$URL = "https://www.yourwebserver.com/path/";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
We are attempting to post to a rest API endpoint that requires a "POST object" within one of the keys. In javascript/jquery, this works fine. But, using CURL in PHP the endpoint doesn't receive the object (called "components") here:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'http://api.sitesitesite.com');
$comps = array('slug' => "xyz",
'visble' => 1,
'color' => "xyz",
'shape' => "xyz",
'version' => "2",
);
$post_args = array();
$post_args['components'] = $comps;
$post_args['id'] = $id;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Get rid of "CURLOPT_CUSTOMREQUEST" and use "CURLOPT_POST"
curl_setopt($ch, CURLOPT_POST, true);
I want executed this simple curl operation through php how can i execute this in simple php not any framework , can i do it in simple php or do i need the framework?
curl -XPOST localhost:12060/repository/schema/fieldType -H 'Content-Type: application/json' -d '
{
action: "create",
fieldType: {
name: "n$name",
valueType: { primitive: "STRING" },
scope: "versioned",
namespaces: { "my.demo": "n" }
}
}' -D -
what i tried is :
<?php
$url="localhost:12060/repository/schema/fieldType";
//open connection
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
$fieldString=array(
"action"=> "create",
"fieldType"=>array(
"name"=> "n$name",
"valueType"=> array( "primitive"=> "STRING" ),
"scope"=> "versioned",
"namespaces"=> array( "my.demo"=> "n" )
)
);
//for some diff
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{json: $fieldString}"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt_array( $ch, $options );
//execute post
$result = curl_exec($ch);
$header = curl_getinfo( $ch );
echo $result;
//close connection
curl_close($ch);
?>
But it is giving me this
The given resource variant is not supported.Please use one of the following: * Variant[mediaType=application/json, language=null, encoding=null]
Your problem lies here:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{json: $fieldString}"));
The $fieldString is not actually an string as its name implies. It was an array at this point still. And you are trying to reencode an mangled faux json string, with just Array as data. Won't work.
Instead use this to get the desired(?) effect:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fieldString));