$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
Related
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.
I am using laravel as a server API backEnd but when clients send request using cURL I got the following json decode exception
JSON_ERROR_SYNTAX
here is the code of client
$data = [ "items"=>array(
"order_reference"=> "123412",
"mode" =>"payment",
"products" => [array(
"id"=> 1,
"product_name"=> "product 1",
"quantity"=>1,
"unit_amount"=> 100)
]
, "currency"=>"YER",
"total_amount"=>1500,
"success_url"=> "https://company.com/success",
"cancel_url"=> "https://company.com/cancel",
"metadata"=>[
"Customer name"=> "somename",
"order id"=> 0 ]
)];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/api/order",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
),
));
$response = curl_exec($curl);
and this is the code of API backEnd
json_decode($request->items,true);
and the result of the code is
JSON_ERROR_SYNTAX
It should be json_decode($response->items,true);
Add this header
Content-Type: application/json
to your client code in this manner:
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
"Content-Type: application/json"
)
then laravel will automatically decode in the backend and you don't need to use json_decode. You can simply use $request->items to access to the client's items.
I got the issue in integration of route mobile sms gateway. In URL i send the message but URL does not accept space so encode the message by using encode function, then URL accept the message but after using encode, SMS also receive as encoded.
please check the code.
$text = "" . $sms_message . " " . $user_data['otp'] . "";
$message = $this->myUrlEncode($text);
$URL = "http://rslr.connectbind.com:8080/bulksms/bulksms?username=$username&password=$password&type=0&dlr=1&destination=$number&source=$source&message=$message";
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => $URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 30,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Connection: keep-alive",
"Host: rslr.connectbind.com:8080",
),
));
$response = curl_exec($curl);
So, what should I do to solve this problem?
Not sure what your encode function does, but if you just wrap ‘urlencode’ you can use ‘urldecode’.
$response = urldecode(curl_exec($curl));
Hi im trying to use a custom form on my website to add a new contact to our marketing list, each contact will contain an email and first name.
Im trying to follow this documentation but am having no success:
https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact
I have tried using their tool but it always says incorrect JSON but when i use an online validator it says correct, I can't figure out how to match theirs to get my request to post.
This is my current code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"]}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer SG.OCFHb********P3iuikQ.bqKdM-da7X609ZNo9UT7y*********u5fDlfQo80o",
"content-type: application/json"
),
));
You just should be added } chars after $first_name.
This is valid JSON:
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"}]}",
You can check it with https://jsonformatter.curiousconcept.com/ validator website.
One little trick to not have to deal with all the backslashes and escaping is to use a pre-defined object literal in PHP, by using arrays with (object) in front of them, to define the objects nested in the structures.
make an object, turn it into JSON with json_encode
enter the encoded JSON object into the CURLOPT_POSTFIELDS in the cURL request.
<?php
// make custom fields object for entry into the cURL later on:
// THERE IS NO OBJECT LITERAL IN PHP, but using (object) in front of an array, voila
$contact_info = (object)[
"list_ids" => [
"eeee-eeee-eeee-eeee-eeeeexample" // array of list ids here.
],
"contacts" => [ // this is an array of objects (array[object]), according to the api-docs.
(object)[
"email" => "email#example.com",
"country" => "the country",
"city" => "the city",
"custom_fields" => (object)[
"e1_T" => "sign-up-page-name", // only custom fields use ids as the key.
"e2_T" => "next-custom-field" // keep adding custom fields in this array.
]
]
]
];
// now all we have to do is to encode the object into JSON:
$json_contact_data = json_encode($contact_info);
// now add the contact:
$curl = curl_init();
// update the contact
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json_contact_data, // here we enter the pre-configured json from above.
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $your_api_key . "",
"content-type: application/json"
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$decoded = json_decode($response, true); // decode into associative array.
if ($err) {
echo "cURL Error #: " . $err;
} else {
print_r($decoded); // print the decoded json message.
}
?>
This is the best way:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sendgrid.com/v3/marketing/contacts',
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 =>'{"list_ids": ["list-ID"], "contacts": [{"email": "' . $_GET["email"] . '"}]}',
CURLOPT_HTTPHEADER => array(
': ',
'Authorization: Bearer SG.000000000',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
die();
I pass some coordinates to my foreach to a reverse geocoding. I get only one coordinate but not them all. I think it's a problem of delay.
Here's my code:
foreach ($coordPointsArray as $coordPointArray) {
$latLangCoords = implode(",", $coordPointArray);
echo($latLangCoords);
$callToGoogleGeocode = curl_init();
$urlGeocode = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='. urlencode($latLangCoords). '&key=' . $googleApiKey;
curl_setopt_array($callToGoogleGeocode, [
CURLOPT_URL => $urlGeocode,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"cache-control: no-cache"
],
]);
$rawResponseGeocode = curl_exec($callToGoogleGeocode);
}
return($rawResponseGeocode);
here's the echo response:
45.4807897,9.21359845.4801858,9.214740945.4801858,9.214740945.4763715,9.21103245.4763715,9.21103245.4748449,9.211080445.4748449,9.211080445.4685044,9.211260645.4685044,9.211260645.4673681,9.211252645.4673681,9.211252645.4621453,9.211394845.4621453,9.211394845.4621861,9.20949345.4621861,9.20949345.4604364,9.2092372
I did a mistake. I was overwriting the variable at the end. I needed to store my responses in an array with array push.