Converting cURL request to PHP - php

I've got the following sample cURL request that I'm trying to run through PHP.
curl --request PUT \
--url 'https://example.com/' \
--user 'APIKEY:ACCESSTOKEN' \
--header 'content-type: application/json' \
--data '{"TableId": "MyHats","Columns": ["Id","Name"],"Data": [["1","Hat 1"],["2","Hat 2"],["3","Hat 3"]]}' \
At first I tried converting an old POST request to use PUT and send the JSON but it didn't seem to work.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$access_token");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
I've also tried the flowing PUT request converted from an example I found online
$data_string = json_encode($data);
$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));
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$access_token");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
The API doesn't have a response but it does return a 302 status code which I'm told is the expected result but they aren't receiving any of the data on their end.

Related

Sending a PUT request cURL in PHP 8.1 returns PROTOCOL_ERROR

I have a curl request, which I can send from the cli like this:
curl -X 'PUT' \
'https://XXX/endpiontY' \
-H 'Authorization: Bearer <TOKEN>' \
-H 'Content-Type: application/json' \
-d '{
"value": "TestValue"
}'
(curl --version returns curl 7.81.0, so default is CURL_HTTP_VERSION_2TLS)
I am trying to send the same information with PHP, but I get
HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
My Setup
$json = array('value' => 'TestValue');
$json_string = json_encode($json);
$headers = array(
'Content-Type:application/json',
'Content-Length: ' . strlen($json_string)
)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($ch, CURLOPT_PUT, true);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
}
curl_close($ch);
$result is always false so the error triggers with the above message shown.
I tried
I found this https://stackoverflow.com/a/59955444/1092632 curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
causes a timeout of the endpoint
Using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); instead of curl_setopt($ch, CURLOPT_PUT, true);
no change
Adding $headers[] = 'Connection: close';
no change
Where am I wrong sending the PUT request? Any hint highly appreciated!

Convert cURL to PHP cURL with Authorization: Client-ID in header

Hello i want to convert this command line in php curl
curl -X POST https://api.imgur.com/3/upload -H "Authorization: Client-ID cdeebfbfe0d825a" -F "image=http://IP/output/1530096054.png"
MY PHP code
$URL = 'https://api.imgur.com/3/upload';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
print_r($ch);
print_r($status_code);
Thanx for help
I found the solution
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'image=http://IP/output/1530096054.png');
$headers = array();
$headers[] = 'Authorization: Client-ID 46as4d6as54d6as46';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

cURL Commandline to php version

when I use the following on command line I am getting the output perfectly.
curl -X GET -H "Authorization: sso-key API_KEY:API_SECRET" "https://api.godaddy.com/v1/domains/mydomain.com"
but when I try to get this from PHP using the following code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: sso-key API_KEY:API_SECRET"]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "API_KEY:API_SECRET");
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);
I failed to get anything.
what am I missing here?
$URL = "https://api.godaddy.com/v1/domains/mydomain.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: sso-key API_KEY:API_SECRET"]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result=curl_exec ($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($result);
var_dump($httpCode);
curl_close ($ch);

How to convert command line cURL to PHP cURL?

I have command
curl -v -X POST -H "Content-Type: application/xml" -d #case.xml --user test:test url
in php send
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => new CurlFile('case.xml')]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
but this code not work, error 400 Bad Request.
The web server uses Basic HTTP Authentication. I'm using 7.0.11
<?php
$login='test';
$password='test';
$xml_data =file_get_contents(realpath('case.xml'));
$url ='mega_url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$output = curl_exec($ch);
it is work

Converting command cURL to HTTP request

curl -X POST https://example.com/sandbox -u \
'username:password' -d 'vendor=123456' -d 'list_id=1000001' \
-H 'Accept: application/json
How would I structure a HTTP request with a command cURL like this, with a username/password?
You can use curl in PHP. I've created a example code for you:
$username='username';
$password='password';
$URL='https://example.com/sandbox';
$data=array('vendor'=>123456, 'list_id'=>1000001);
$payload = json_encode( $data );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$result=curl_exec($ch);
curl_close ($ch);
I hope that helps :D

Categories