PHP CURL is not working while converting from curl command - php

i have a curl command, its working fine in terminal and want to convert in PHP code but its giving 415 Unsupported Media Type error.
CURL Command:
curl --location
--request POST 'https://securegw-uat.starhealth.in/api/proposal-service/v2/ckyc/generate'
--header 'APIKEY: 25861f5655bd4640b5518ceb2bfb5f94'
--header 'SECRETKEY: beec6bf3afff4efdab1f6a80d8601fbf'
--form 'bodyJson="{\"ckycId\":10036801290855,\"idOrAddressProofDocumentId\": 1,\"titleId\": 1,\"firstName\": \"Swadhin\",\"middleName\": \"\",\"lastName\": \"Dhal\",\"genderId\": 1,\"birthdate\": \"November 01, 1973\",\"occupationId\": 1,\"residentialStatusId\": 1,\"pan\": \"BLAPT0864M\",\"familyRelationshipId\": 3,\"familyMemberTitleId\": 2,\"familyMemberFirstName\": \"Rachita\",\"familyMemberLastName\": \"Dhal\",\"addressLineOne\": \"No. 20\",\"addressLineTwo\": \"Nehru Street\",\"cityName\": \"Triplicane\",\"districtName\": \"Chennai\",\"stateCode\": \"TN\",\"countryCode\": \"IN\",\"postalCode\": \"600005\",\"incomeSourceId\": 1,\"isAnyonePEP\": 0}";type=application/json'
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://securegw-uat.starhealth.in/api/proposal-service/v2/ckyc/generate',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('bodyJson' => '{
"ckycId": 10036801290855,
"idOrAddressProofDocumentId": 1,
"idOrAddressProofDocumentNumber": "1234",
"titleId": 1,
"firstName": "Swadhin",
"middleName": "",
"lastName": "Dhal",
"genderId": 1,
"birthdate": "November 01, 1973",
"occupationId": 1,
"residentialStatusId": 1,
"pan": "BLAPT0864M",
"familyRelationshipId": 3,
"familyMemberTitleId": 2,
"familyMemberFirstName": "Rachita",
"familyMemberLastName": "Dhal",
"addressLineOne": "No. 20",
"addressLineTwo": "Nehru Street",
"cityName": "Triplicane",
"districtName": "Chennai",
"stateCode": "TN",
"countryCode": "IN",
"postalCode": "600005",
"incomeSourceId": 1,
"isAnyonePEP": 0
}'),
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data',
'APIKEY: 25861f5655bd4640b5518ceb2bfb5f94',
'SECRETKEY: beec6bf3afff4efdab1f6a80d8601fbf'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;`

Please try to specify the header to --header 'Content-Type: multipart/form-data'.

Related

PUT API loop with Json file

I'm building an API using PHP.
I want to PUT data from json file.
And have a json file over 1000 SKU
{"data":[
{
"sku": "ZT006V",
"w_quantities": [
{
"w_id": 460,
"qty": 10},
{
"w_id": 2454,
"qty": 10}
]
},
{
"sku": "ZT006XXX",
"w_quantities": [
{
"w_id": 454,
"qty": 12
}]
}
]}
I do a PUT API command with ti.json content.
And I get error: 20 SKU limit per submission
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '/products/updateSkus',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>file_get_contents('ti.json'),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer xxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl)
curl_close($curl);
echo $response;
I wonder, is there a way to send all 1000+ SKUs in the json file?
I searched the net, but no solution worked for me.
regards
You manipulate the JSON File data on the fly using an array of numbers, which if number presents the ID, if I understand currently.

Unable to create envelopes using DocuSign API error: Object moved

I want to create envelope using DocuSign API in my web application. When I run it in postman then response showing object move with html content
This is my php code sample
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://account-d.docusign.com/v2.1/accounts/b15b77b0-9345-4780-bfc1-440b37991820/envelopes?change_routing_order=true',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"documents": [
{
"documentBase64": "Base64 code of my pdf file",
"documentId": "5865888",
"fileExtension": "pdf",
"name": "test pdf"
}
],
"emailSubject": "test pdf",
"recipients": {
"signers": [
{
"name": "User full name",
"email": "user#gmail.com",
"recipientId": "8959555"
}
]
},
"status": "send "
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: [{"key":"Authorization","value":"Bearer {{accessToken}}"}]'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
My API response is
Object moved to here.
Please guide me how to solve the issue
Thank You
That base URL is incorrect. Account-d.docusign.com is only for authentication.
Requests should be made to {SERVER}/restapi/v2.1
So for you it would be demo.docusign.net/restapi/v2.1/accounts/{ACCOUNT_ID/envelopes
Here's the dev centre article showing the whole process

Passing Variable to Curl Post Field

I want to pass dynamic value stored in variable named as $order2, I want to pass that value to postfield using CURL Request.
<?php
$order2 = "1604";
$curl2 = curl_init();
curl_setopt_array($curl2, array(
CURLOPT_URL => "https://ssapi.shipstation.com/orders/createlabelfororder",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n \"orderId\": $order2 ,\n \"carrierCode\": \"stamps_com\",\n \"serviceCode\": \"usps_priority_mail\",\n \"packageCode\": \"small_flat_rate_box\",\n \"confirmation\": null,\n \"shipDate\": \"2021-11-05\",\n \"weight\": {\n \"value\": 2,\n \"units\": \"pounds\"\n },\n \"dimensions\": null,\n \"insuranceOptions\": null,\n \"internationalOptions\": null,\n \"advancedOptions\": null,\n \"testLabel\": true\n}",
CURLOPT_HTTPHEADER => array(
"Host: ssapi.shipstation.com",
"Authorization:Basic MYAUTHCODE",
"Content-Type: application/json"
),
));
$response2 = curl_exec($curl2);
curl_close($curl2);
echo $response2;
It is returning me following response :
{"Message":"The request is
invalid.","ModelState":{"request.orderId":["Error reading integer.
Unexpected token: Undefined. Path 'orderId', line 2, position 15."]}}

Curl iteratively pass multiple payloads from json or array

How do I pass multidimensional associative array or a json as a payload to cURL request. I have been trying to wrap my head around this for sometime now without any success.
Here is what I have done so far. I make a curl request and pass a json to the CURLOPT_POSTFIELDS fields. This works alright. The problem is I want the code to run for multiple users which are in a json string saved in a variable
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url/tokens',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"customerName": "orland",
"mno": "Network",
"amount": "1",
"msisdn": "447911123456",
"description": "Awaiting",
"reference": "0fgdufgdfgdfs"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The above code works fine.
But the payload for CURLOPT_POSTFIELDS is not fixed.
So this is what I tried doing, I encoded the json string and tried passing it to a for loop. I am sure the approach isn't bad, but I am not getting the syntax quite right. Here is my code.
<?php
$recip = '[{
"customerName": "Sorland",
"mno": "Network",
"amount": "1",
"msisdn": "447911123346",
"description": "Awaiting",
"reference": "0fgdufgdfgdfs"
},
{
"customerName": "Corland",
"mno": "MTN",
"amount": 1,
"msisdn": "447911123678",
"description": "Awaiting",
"reference": "0jsbfbsubfhbj"
},
{
"customerName": "orland",
"mno": "MTN",
"amount": 1,
"msisdn": "447911123111",
"description": "Awaiting",
"reference": "1234568djnfjnfjds"
}]';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url.com/api/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>
echo ("'{<br>");
foreach($arr[$keys[$i]] as $key => $value) {
echo implode($key . " : " . $value . ",<br>");
}
echo "}',<br>";
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
I want to use a for loop to make multiple curl request with different payloads, and the payloads are coming from the json data above. I converted the json to an array to be able to loop through and converted it back to a json for CURLOPT_POSTFIELDS.
This seems a simpler way to make multiple call to curl and save the responses, hope I got the right end of the stick this time :)
$recip =
'[
{"customerName": "Sorland", "mno": "Network", "amount": "1",
"msisdn": "447911123346","description": "Awaiting","reference": "0fgdufgdfgdfs"
},
{"customerName": "Corland","mno": "MTN","amount": 1,
"msisdn": "447911123678","description": "Awaiting","reference": "0jsbfbsubfhbj"
},
{"customerName": "orland","mno": "MTN","amount": 1,"msisdn": "447911123111",
"description": "Awaiting","reference": "1234568djnfjnfjds"
}
]';
// make a PHP datatype out of the JSON so you can use it
$recipients = json_decode($recip);
$responses = [];
foreach ( $recipients as $r) {
$curl = curl_init();
// convert $r back to json so it can be passed as param
$postfield = json_encode($r);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url.com/api/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postfield,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$response[] = curl_exec($curl);
// if you need to know which customer the response was for
// use this next line instead, for example
//$response[$r->customerName] = curl_exec($curl);
curl_close($curl);
}
// this is now an array of all responses
print_r($responses);

How do I correctly concatenate this php string

$name = $_POST["fname"];
echo $name;
CURLOPT_POSTFIELDS => "{\n
\"DisplayName\": \" ".$name." \",\n
\"PrimaryEmailAddr\": {\n \"Address\": \"jdrew#myemail.com\"\n }\n}\n",
I am trying to make a POST request with the details in the form which actioned the php file,
the echo prints the correct information, ive tried + and . and { and "" and all manner of things to no avail, hopefully anyone with an ounce of know how can fix this one , this is m first time near php :/
edit:
$jsons = array(
"DisplayName"=> "scsKing's Groceries",
"Suffix"=> "Jr",
"Title"=> "Mr",
"MiddleName"=> "B",
"Notes"=> "Here are other details.",
"FamilyName"=> "King",
"GivenName"=> "James"
);
$JsonString = json_encode($jsons);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://quickbooks.api.intuit.com/v3/company/123146505733159/customer?minorversion=41",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $JsonString,
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Encoding: gzip, deflate",
Would this be the correct format, it returns cURL Error #:Operation timed out after 30000 milliseconds with 0 bytes received
thanks

Categories