Convert a specific cURL command to Php cURL - php

I am having issues with this cURL command. I wanted to send request via Php.
Here Goes my cURL command:
curl -i -s -k -X 'POST' \
-H 'Origin: http://117.239.83.200:2020' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Linux; Android 5.1.1; A0001 Build/LMY48B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Referer: http://117.239.83.200:2020/frmUniversityResult.aspx' \
--data-binary $'__LASTFOCUS=&__EVENTTARGET=ddlSem&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTIxMTE5MjUyMzYPFgIeDFByZXZpb3VzUGFnZQUzaHR0cDovLzExNy4yMzkuODMuMjAwOjIwMjAvZnJtVW5pdmVyc2l0eVJlc3VsdC5hc3B4FgICAw9kFgICAQ9kFgRmD2QWDAIFDxAPFgYeDURhdGFUZXh0RmllbGQFBUFsaWFzHg5EYXRhVmFsdWVGaWVsZAULSW5zdGl0dXRlSUQeC18hRGF0YUJvdW5kZ2QQFQkJU2VsZWN0Li4uBUNTUElUBkNNUElDQQRSUENQBElJSU0GUERQSUFTBEFSSVAETVRJTgRDSVBTFQkBMAExATIBMwE0ATUBNgIxNgIxORQrAwlnZ2dnZ2dnZ2cWAQIBZAIHDxAPFgYfAQUKRGVncmVlQ29kZR8CBQhEZWdyZWVJRB8DZ2QQFRcJU2VsZWN0Li4uCUJURUNIKENFKQlCVEVDSChDTCkJQlRFQ0goRUMpCUJURUNIKEVFKQlCVEVDSChJVCkJQlRFQ0goTUUpBERSQ0UERFJDTAREUkVDBERSRUUFRFJMU0MERFJNRQVEUk1USApNVEVDSChBTVQpCU1URUNIKENFKQlNVEVDSChDTCkJTVRFQ0goRUMpCU1URUNIKEVFKQpNVEVDSChFVkQpCU1URUNIKElUKQlNVEVDSChNRSkDTVRNFRcBMAIzOQI0MQI0MAIzNwIzOAIzNgI4MgI5MAI3MAI3MgI4MwI3MQI3NQMxMDYCNjUDMTA1AjYxAjg3AzExNgI5NQI2MAMxMTAUKwMXZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cWAQIBZAIJDxAPFgYfAQUDU2VtHwIFA1NlbR8DZ2QQFQkJU2VsZWN0Li4uATEBMgEzATQBNQE2ATcBOBUJATABMQEyATMBNAE1ATYBNwE4FCsDCWdnZ2dnZ2dnZxYBZmQCCw8QDxYGHwEFDUV4YW1Nb250aFllYXIfAgUOU2NoZWR1bGVFeGFtSUQfA2dkEBUFCVNlbGVjdC4uLghNQVkgMjAxNQ1ERUNFTUJFUiAyMDE0CE1BWSAyMDE0DU5PVkVNQkVSIDIwMTMVBQEwBDIyMDAEMjA1MgQyMDA2BDE4NDEUKwMFZ2dnZ2dkZAIPDw9kFgIeB29uY2xpY2sFUCB0aGlzLnZhbHVlPSJQcm9jZXNzaW5nLi4iIDt0aGlzLmRpc2FibGVkID0gdHJ1ZTsgX19kb1Bvc3RCYWNrKCdidG5TZWFyY2gnLCcnKSA7ZAIRDw8WAh4EVGV4dGVkZAIBD2QWAgIBD2QWAgITDzwrAA0AZBgCBQhtdlJlc3VsdA8PZGZkBRF1Y2xHcmQxJGdyZFJlc3VsdA9nZFkzbjDOCTYtz8GQYcnnUncb54ui&__VIEWSTATEGENERATOR=F859A3E9&ddlInst=1&ddlDegree=39&ddlSem=1&ddlScheduleExam=0&txtEnrNo=' \
'http://117.239.83.200:2020/frmUniversityResult.aspx''
And, here is my approach in Php.
<?php
$url = "http://117.239.83.200:2020/frmUniversityResult.aspx?ddlInst=1&ddlDegree=39&ddlSem=1&ddlScheduleExam=0&txtEnrNo=";
$postfields = array('__EVENTTARGET' => 'btnSearch',
'__VIEWSTATEGENERATOR' => 'F859A3E9',
'ddlInst' => '1',
'ddlDegree' => '39',
'ddlSem' => '3',
'ddlScheduleExam' => '2380',
'txtEnrNo' => '14ce055'
);
$postdata = http_build_query($postfields);
//var_dump($postdata);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
But unfortunately, It redirects me to its Index page. Can someone pls help me out with this?

Related

cURLnot working properly from PHP or CRON

I have nn.sh
#/bin/sh
cd /var/www/html
/usr/bin/curl 'https://web-api.wtnnip.com/app' \
-H 'authority: web-api.wtnnip.com' \
-H 'accept: */*' \
-H 'accept-language: ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'content-type: application/json' \
-H 'origin: https://www.wtnnip.com' \
-H 'referer: https://www.wtnnip.com/ro-RO/non-professional' \
-H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Linux"' \
-H 'sec-fetch-dest: empty' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-site: same-site' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
--compressed
If I execute the file is working fine. If I copy cURL command and run in terminal, is working fine. Working fine means I receive the json collection.
But if I set crontab to run this file, or I run the file from PHP with exec or passthru I receive this error:
{"errors" : [ {
"message" : "Invalid JSON payload in GraphQLAuth POST request.",
"errorType" : "MalformedHttpRequestException"
} ]
}
I also try to write a code and use the cURL directly from PHP, but I receive the same error.
How can I run this from cron or php? Thanks!
LE:
I use Debian 11, PHP 7.4 and cURL 7.74
I have bash as shell, I try with #/bin/bash as well
SOLVED
#!/bin/bash in first line
I forgot the exclamation mark.
Thanks to Ulrich Eckhardt for light!
#!/bin/sh
cd /var/www/html || exit
If you want to try PHP, Try this:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://web-api.wtnnip.com/app');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'authority' => 'web-api.wtnnip.com',
'accept' => '*/*',
'accept-language' => 'ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7',
'content-type' => 'application/json',
'origin' => 'https://www.wtnnip.com',
'referer' => 'https://www.wtnnip.com/ro-RO/non-professional',
'sec-ch-ua' => '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
'sec-ch-ua-mobile' => '?0',
'sec-ch-ua-platform' => '"Linux"',
'sec-fetch-dest' => 'empty',
'sec-fetch-mode' => 'cors',
'sec-fetch-site' => 'same-site',
'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
'Accept-Encoding' => 'gzip',
]);
$response = curl_exec($ch);
curl_close($ch);

Why bitbucket server checks Token, even if its blocked?

we have Atlassian Bitbucket Server. And I'm trying to use its API.
Found this: https://docs.atlassian.com/bitbucket-server/rest/7.21.0/bitbucket-rest.html#idp299
Using PHP, i tried:
$url = "http://10.77.78.235:7990/rest/api/1.0/projects/HAL/repos/crm-2/branches";
$headers = array(
'cache-control: max-age=0',
'upgrade-insecure-requests: 1',
'user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
'sec-fetch-user: ?1',
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'x-compress: null',
'sec-fetch-site: none',
'sec-fetch-mode: navigate',
'accept-encoding: deflate, br',
'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
"Content-Type: application/json",
'X-Atlassian-Token: no-check',
);
$post_data = array (
"name" => "bar",
"startPoint" => "52005525378526ac3d5e5bcffc48fc9a82ebca76",
"message" => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "myuser:mypassword");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
And the answer i recieve is:
XSRF check failed
Who knows why - please help)
Kinda stupid solution:
I just had to remove this:
'user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
From headers.

"Invalid JSON payload received" Error when I send a command to the SDM API for a Google Nest Device using PHP curl

I like to use the command function of the SDM API. I have to do it in PHP
The Google Documentation requires the following Syntax :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer access-token' \
--data-raw '{
"command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
"params" : {}
}'
My PHP Code looks like this :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
This command generates the following Error (I replaced the Event ID with XYZ) :
Array
(
[error] => Array
(
[code] => 400
[message] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ...."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
[status] => INVALID_ARGUMENT
[details] => Array
(
[0] => Array
(
[#type] => type.googleapis.com/google.rpc.BadRequest
[fieldViolations] => Array
(
[0] => Array
(
[description] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ..."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
)
)
)
)
)
)
I have done the following Test :
Google provides a Terminal function which allows me to enter the original syntax described in the Documentation (see above) without relying on PHP Curl.
After a successful manual Test I used my PHP code to create the RAW Data :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
and the URL :
$url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand';
I manually copied the strings into this structure :
curl -X POST \
'' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ' \
--data-raw ''
I then copy paste the following code into the Terminal and it worked (sensitive Info deleted) :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/f779e361..../devices/AVP.......:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ya29........' \
--data-raw '{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"Ci........."}}'
So the error must originate from my Curl Code :
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
OK Problem is fixed. My Headers where wrong. The correct Curl Code is this :
$curl_handle=curl_init();
$headers = array(
"POST HTTP/1.0",
"Content-type: application/json",
"Accept: application/json",
"Content-length: ".strlen($PostData),
"Authorization: Bearer ".$access_token
);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
Tks everybody for your response, it helped me to move in the correct direction

How do i convert this cURL command into PHP code? And what is "Request Payload"?

So, I was inspecting the network while I'm uploading an JPG image.
A copy as a curl command :
curl "https://admin-official.line.me/11702069/home/api/objects" -H "Origin: https://admin-official.line.me" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBv1B9k1i89zlgjnA" -H "Accept: */*" -H "Referer: https://admin-official.line.me/11702069/home/send/" -H "Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser=^{^\^"id^\^":^\^"^\^"^}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=XXX; minesota=XXX; minesota2=XXX-jdT5f_x10ov7Re12NliQw_zFyIe9osY4oOOc2Rxdy1gMyBhXbKdPmnpA2LHB9VhvlqlL_4hkeSNQK5SgU; plack_session=6cd2e4790ae9990303ea54f7f35b305fba801dc0; _trmcsession=^{^\^"id^\^":^\^"b8ba0724^\^",^\^"path^\^":^\^"/11702069/home/send/^\^",^\^"query^\^":^\^"^\^",^\^"time^\^":1526735963213,^\^"_dice^\^":0.09452730791211028^}; __try__=1526737592637; _trmcpage=/11702069/home/send/" -H "Connection: keep-alive" --data-binary ^"------WebKitFormBoundaryBv1B9k1i89zlgjnA^
Content-Disposition: form-data; name=^\^"file^\^"; filename=^\^"alpen.jpg^\^"^
Content-Type: image/jpeg^
^
^
------WebKitFormBoundaryBv1B9k1i89zlgjnA^
Content-Disposition: form-data; name=^\^"csrf_token^\^"^
^
vJiSf8AZPpAvqCp0JQtrNOMgrZRtrDpa1TEHbYLz^
------WebKitFormBoundaryBv1B9k1i89zlgjnA--^
^" --compressed
I've never seen this before, i'm not an expert at curl. But I'm using curl to PHP coverter, but it's not working in this case.
EDIT : here's what I've tried.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://admin-official.line.me/11702069/home/api/objects");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => "#".realpath('test.jpg'), "csrf_token" => $split2[0]));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = "Origin: https://admin-official.line.me";
$headers[] = "Accept-Encoding: gzip, deflate, br";
$headers[] = "Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6";
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
$headers[] = "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBv1B9k1i89zlgjnA";
$headers[] = "Accept: */*";
$headers[] = "Referer: https://admin-official.line.me/11702069/home/send/";
$headers[] = 'Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser={"id":""}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=XXX; minesota=XXX; minesota2=XXX; plack_session=XXX; _trmcsession={"id":"b8ba0724","path":"/11702069/home/send/","query":"","time":1526735963213,"_dice":0.09452730791211028}; __try__=1526737592637; _trmcpage=/11702069/home/send/';
$headers[] = "Connection: keep-alive";
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
?>
the "request payload" is the request body. a HTTP request is split into 2 parts, the headers, and the body (and Chrome calls the body for the request payload here, not sure if that's an official name, or just the whim of a chromium dev)
anyway, you're using the # method, which was discouraged as of PHP 5.5, disabled-by-default in php 5.6, and completely removed in PHP7.
use CURLFile instead, eg:
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"file" => new CURLFile('test.jpg'),
"csrf_token" => $split2[0]
));
also, don't set the Accept-Encoding header manually. you specify gzip, deflate, br, and curl doesn't support br, and depending on how libcurl was compiled, might not even support gzip/deflate (although it usually does), remove that.
also, set CURLOPT_ENCODING to emptystring, and curl will list all encodings it was built to support (which is usually gzip and deflate, but is not guaranteed to be)
also, when using the # or CURLFile method, don't set the Content-Type header manually, because if curl's boundary doesn't match what you set in the Content-Type header, the server won't be able to read the request properly. curl will set this header for you, if you don't override it, so don't.
Figure it out using Postman.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://admin-official.line.me/11702069/home/api/objects",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
"csrf_token" => $split2[0],
"file" => new CURLFile("test.jpg")
),
//CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"csrf_token\"\r\n\r\nETukCyg_-OM72cZ0MuAyunvvy4cb8Cx2QqoP41T7\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: multipart/form-data",
"Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser=^{^\^id^^:^\^^^^}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=COOKIE; minesota=COOKIE; minesota2=COOKIE; _trmcsession=^{^\^id^^:^\^36b6efe6^^,^\^path^^:^\^/11702069/home/send/^^,^\^query^^:^\^^^,^\^time^^:1526880852499,^\^_dice^^:0.4099096438552394^}; plack_session=519cce226206bd15cba86f2153c9dc91a56df4fb; _trmcpage=/11702069/home/send/; __try__=1526881572577",
"Origin: https://admin-official.line.me",
"Postman-Token: 8b9dc9c8-166a-4c50-93c4-b16ca72d72dc",
"Referer: https://admin-official.line.me/11702069/home/send/",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
//"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Converting a cURL command to PHP

I have the following cURL command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json; charset=utf-8" -H "TC: 000000" -H "ST: 000000" -X POST -d '[{"Param1" : "Value1", "Param2" : "Value2"} ]' http://www.somesaasprovider.com/
It works fine when I'm running it in the terminal and I get the expected output.
However, when I try to use the same command but adapted to PHP, I get no result.
The PHP version looks like this:
error_reporting(E_ALL);
ini_set('display_errors', '1');
$ch = curl_init('http://www.somesaasprovider.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"Param1" : "Value1", "Param2" : "Value2"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'TC' => '000000',
'ST' => '000000')
);
$response = curl_exec($ch);
$response = json_decode($response);
print_r($response);
What am I doing wrong?
Thanks.

Categories