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);
Related
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);
I have the following post api request, the example provided was using cURL command:
curl "https://muserver.com/api/gettoken" --request POST --include -
-header "Content-Type: application/json" --user test:test
I am trying to perform this request using cURL in PHP but getting authentication error.
following is what I have tried:
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
I think I should pass the user and pass. in the form of
--user test:test
but really don't know how to do it.
You seem to be lacking the $username and $password variables :
$username='test';
$password='test';
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
<?php
$url_simple_auth = 'https://www.some_url_need_basic_auth_first.com';
$url_download = 'https://www.some_url_need_basic_auth_first.com/dowonload_file.zip';
$username = 'some_login';
$password = 'some_pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_simple_auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// login simple auth
$simple_auth = curl_exec($ch);
// download file
curl_setopt($ch, CURLOPT_REFERER, $url_simple_auth);
curl_setopt($ch, CURLOPT_URL, $url_download);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=$username&key=$password");
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status_code);
var_dump($result);
This is what I am trying
$url = "https://aon-test.yui.com/api/".$this->_token."/".$action;
$headers = array(
'Content-Type:application/xml' ,
'Authorization: Basic '. base64_encode("s!YIYIYIYIUYIY:X")
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 30000);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xml));
$content = curl_exec($ch);
curl_close($ch);
I am getting 500 - Internal server error.
What mistake I am doing here?
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
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.