I'm struggling using cURL within PHP. I'm not sure what I need to do to "translate" this:
curl -X POST -u "{username}:{password}" --header "Content-Type: application/json" --data-binary #profile.json "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"
into PHP to execute it there.
This is all I've got so far, but I'm feeling like I'm not even close:
$url2 = 'https://watson-api-explorer.mybluemix.net/personality-insights/api/v3/profile?raw_scores=false&csv_headers=false&consumption_preferences=true&version=2017-02-01';
$request_headers = array();
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-Type: text/plain';
$request_headers[] = 'Content-Language: en';
$request_headers[] = 'Accept-Language: en';
$ch2 = curl_init( $url2 );
curl_setopt( $ch2, CURLOPT_POST, 1);
curl_setopt( $ch2, CURLOPT_POSTFIELDS, $myvars2);
curl_setopt( $ch2, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch2, CURLOPT_HEADER, $request_headers);
curl_setopt( $ch2, CURLOPT_RETURNTRANSFER, 1);
$response2 = curl_exec( $ch2 );
var_dump($response2);
It looks like you are just missing the authentication piece:
curl_setopt( $ch2, CURLOPT_USERPWD, "yourUsername:yourPassword");
Check out the manual. Also, you can do it this way, which can be a little easier:
curl_setopt_array( $ch2, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $myvars2,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HEADER => $request_headers,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERPWD => 'yourUsername:yourPassword'
);
Related
I need to send a POST request to Cloudflare API,
Their API example is:
curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}
I have been trying from my functions.php file to make a POST request. Here is my code:
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, 'my_url' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array(
'X-Auth-Email' => 'my_email',
'X-Auth-Key' => 'cf_api_key',
'Content-Type' => 'application/json',
));
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode( '{"purge_everything":true}' ));
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
I am getting this response
'''{"success":false,"errors":[{"code":9106,"message":"Missing X-Auth-Key, X-Auth-Email or Authorization headers"}]} bool(true) '''
Where and how should I put my X-Auth-Key etc?
you have to use the array of httpheader as single values, separeted with doublepoint, not as key value pair:
please see this example:
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, 'my_url' );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: my_email',
'X-Auth-Key: cf_api_key',
'Content-Type: application/json',
));
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, ["purge_everything"=>true] );
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
I have the following php curl command:
// GET TOKEN
$ch = curl_init('https://api.meinbuero.de/openapi/auth/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('ownershipId' => $wisoOwnershipId)) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
$myToken = $response['token'];
// GET CUSTOMER
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'Authorization: Bearer '.$myToken));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, 'https://api.meinbuero.de/openapi/customer?'.http_build_query(
array(
'offset' => 0,
'limit' => 10,
'orderBy' => 'name',
'desc' => 'false',
'search' => ''
)));
$response = json_decode(curl_exec($ch),true);
curl_close($ch);
echo $response;
Now I need this curl commands (for testing) as a command which I can send via terminal.
TO get the toke I tried this successfully:
curl -X POST "https://api.meinbuero.de/openapi/auth/token" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"ownershipId\":\"XYZ\"}" -u WISO_API_KEY:WISO_SECRET_API_KEY
But than I would like to get the customers:
curl -X GET "https://api.meinbuero.de/openapi/customer?offset=0&search=&limit=20&orderBy=title&desc=true" -H "accept: application/json" -H "Content-Type: application/json" -u WISO_API_KEY:WISO_SECRET_API_KEY -H "Authorization: Bearer MY_TOKEN"
I get the message:
Unauthorized%
I am trying to create a HTTP PUT request with cURL and I can't make it work. I've read many tutorials but none of them actually worked. Here's my current code:
$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);
if (curl_error($ch))
{
print curl_error($ch);
}
else
{
print 'ret: ' .$returned;
}
I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!
Just been doing that myself today... here is code I have working for me...
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Using Postman for Chrome, selecting CODE you get this... And works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://blablabla.com/comorl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"jordi#correo.es\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"x-api-key: whateveriyouneedinyourheader"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
In a POST method, you can put an array. However, in a PUT method, you should use http_build_query to build the params like this:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
You have mixed 2 standard.
The error is in $header = "Content-Type: multipart/form-data; boundary='123456f'";
The function http_build_query($filedata) is only for "Content-Type: application/x-www-form-urlencoded", or none.
I'm trying to do this curl request using php http-build-query but it is not working.
curl -H "Content-Type: application/json" -X POST -d '{"crawlDepth": 1, "url": "http://testphp.vulnweb.com/artists.php?artist=1"}' http://127.0.0.1:8775/scan/
How is the best way to create this request?
$data = array("crawlDepth" => 1, "url" => "http://testphp.vulnweb.com/artists.php?artist=1");
$data_string = json_encode($data);
$ch = curl_init('http://127.0.0.1:8775/scan/');
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);
I am trying to create a HTTP PUT request with cURL and I can't make it work. I've read many tutorials but none of them actually worked. Here's my current code:
$filedata = array('metadata' => $rdfxml);
$ch = curl_init($url);
$header = "Content-Type: multipart/form-data; boundary='123456f'";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($filedata));
$returned = curl_exec($ch);
if (curl_error($ch))
{
print curl_error($ch);
}
else
{
print 'ret: ' .$returned;
}
I've also tried using PHP PEAR but got the same result. The problem is that the repository says that no metadata has been set. I really need help! Thanks!
Just been doing that myself today... here is code I have working for me...
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
Using Postman for Chrome, selecting CODE you get this... And works
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://blablabla.com/comorl",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\n \"customer\" : \"con\",\n \"customerID\" : \"5108\",\n \"customerEmail\" : \"jordi#correo.es\",\n \"Phone\" : \"34600000000\",\n \"Active\" : false,\n \"AudioWelcome\" : \"https://audio.com/welcome-defecto-es.mp3\"\n\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"x-api-key: whateveriyouneedinyourheader"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
In a POST method, you can put an array. However, in a PUT method, you should use http_build_query to build the params like this:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $postArr ) );
You have mixed 2 standard.
The error is in $header = "Content-Type: multipart/form-data; boundary='123456f'";
The function http_build_query($filedata) is only for "Content-Type: application/x-www-form-urlencoded", or none.