I have an issue trying to convert a python GET request to php curl.
This is what the python command looks like :
requests.get( f'{url}/report', data={'token': reporting_token, 'format': 'pdf'} )
I'm trying to do the same thing in php curl :
$ch = curl_init();
$params = array(
'token'=>'abcdefgh',
'format'=>'pdf',
)
$url = $url.'?'.http_build_query($params); //this shows http://xxx/report?token=abcdefgh&format=pdf
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
This couldn't be any simpler, however I am getting the following error :
{"message":"missing parameter : token"}
OK here's the fix for anyone interested, the idea is to produce the curl command:
curl -GET -d 'token=abcdefg'
This can be achieved in php curl forcing a GET providing post fields...
curl_setopt($ch, CURLOPT_POSTFIELDS, $get);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
Related
i need help i've been trying to run this
curl -H "Authorization: Basic dXNlckBjb21wYW55LmNvbTp0ZXN0" https://security.voluum.com/login
but quite stuck, also is it possible to create this in php or in json? i need data to create something. also i tried replacing dXNlckBjb21wYW55LmNvbTp0ZXN0 with my own 64bit username:pass but still not working can someone explain this i really dont know what to do
In your case data for login is: user#company.com:test. And you can use curl in ssh like follows:
curl -u user#company.com:test https://security.voluum.com/login
PHP
You can also execute this in PHP as follows:
$user='user#company.com';
$pass='test';
$url='https://security.voluum.com/login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec($ch);
curl_close($ch);
echo "Status code: $status_code \n";
print_r($result);
More informations
Base64
dXNlckBjb21wYW55LmNvbTp0ZXN0 == user#company.com:test you can decode/encode it by yourself here
Curl
More about -u you can read here. And curl manual is available here
PHP
PHP:Curl Manual
I'm trying to convert command-line cURL to PHP cURL, but it doesn't seem to work when I do it through PHP. I access the same URL and everything, but the API returns a general error with no data.
This works properly, it is accessed via POST with no post body, just parameters in the URL.
$curlRequest = 'curl -X POST -d "" --globoff \''.$baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
exec($curlRequest, $result);
However, when I try to perform the same request with the cURL library in PHP, it doesn't work.
$url = $baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$server_output = curl_exec($ch);
curl_close ($ch);
$server_output just ends up being an empty error from the API instead of the same data that the first code block returns. Am I doing something incorrectly? Are the two requests not equivalent?
Hi i am trying to upload using this tutorial http://ge.tt/developers/start .On step 4 they mention something like this
curl --upload-file myfile.txt http://blobs.ge.tt/a1b2c3/myfile.txt?sig=-TR2k2-3kjsh9nfmn4
What is the equivalent php code for the above line ? i tried below code , but its not working (returning bool false)
$url = $arr["posturl"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$postData = array(
'file' => '#/home/nextgen/public_html/api/myfile.txt',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
var_dump($response);
note : i got correct $url and myfile.txt exists and i tried replacing 'file' => '#/home/nextgen/public_html/api/myfile.txt' with '#myfile.txt' ..nothing seems to work.
'#myfile.txt' must be '#/full/path/to/myfile.txt'
curl_setopt($ch, CURLOPT_POST,1); is required
If you'd used curl's --libcurl option you would've seen the difference. Your command line does PUT, your PHP version does multipart formpost...
I've found at the StackOverflow answer here exactly how I want to post data to another web url.
However, I want this command to be executed in my php web script, not from the terminal. From looking at the curl documentation, I think it should be something along the lines of:
<?php
$url = "http://myurl.com";
$myjson = "{\"column1\":\"1\",\"colum2\":\"2\",\"column3\":\"3\",\"column4\":\"4\",\"column5\":\"5\",\"column6\":\"6\"}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_close($ch);
?>
What is the best way to do that?
The current terminal command that is working is:
curl -X POST -H "Content-Type: application/json" -d '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}' https://myurl.com
There are two problems:
You need to properly quote $myjson. Read the difference between single and double quoted strings in PHP.
You are not sending the curl request: curl_exec()
This should move you in the right direction:
<?php
$url = 'http://myurl.com';
$myjson = '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);
I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:
cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=#-;type=application/json' https://my.url.here
However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:
$post = array(
"exchangeInstance" => $json_string,
"type" => "application/json",
);
$url = 'myurlhere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($post);
var_dump($result);
echo $result;
var_dump($info);
As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.
However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?
curl -F !== -d
$post = array(
"exchangeInstance" => sprintf('#%s;type=application/json', $json_string),
);