cURL: wait for response until it is processed by api - php

everybody,
I am using the following code for synchronizing data with an API ($method= POST) and then I call using the GET method ($method= GET) to print the result of the synchronization:
function sendCurl ($fileName, $method)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($fileName)),
CURLOPT_HTTPHEADER => array(
"Authorization: xxxxxxxxxxxxxxx",
"Accept: application/json."
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
The problem is that the GET method response arrives before the API has time to process it. Therefore, when I print the response I print WAITING FOR RESPONSE and not whether it went ok or failed.
What can I do to print the ok or fail and not the WAITING FOR RESPONSE?
Thank you very much!

Related

Amazon SP API - createFeedDocument

The goal is to do a "POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA" so right now I'm trying to do a createFeedDocument which I believe is the 1st step.
There's few questions about this but none in PHP curl.
I spent hours....HOURS and still can't figure it out..
I get error:
"message": null,
"code": "InvalidInput"
I even tried removing "contentType" from the URL and put it in the header, but then I get error msg: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method."
Please....please is there anyone out there that knows what I need to change with code below?
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://sellingpartnerapi-na.amazon.com/feeds/2021-06-30/documents?contentType=text/tab-separated-values;charset=UTF-8',
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_HTTPHEADER => array(
'Accept: application/json',
'x-amz-access-token: the token<hidden>',
'X-Amz-Date: 20230125T073105Z',
'Authorization: AWS4-HMAC-SHA256 Credential=The_AccessKey/20230125/us-east-1/execute-api/aws4_request, SignedHeaders=accept;host;x-amz-access-token;x-amz-date, Signature=a6e8bdf306ea24438978a5fff961d08fc65d06c10f43c782af15a6a60<hidden>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

PHP cURL Content-Length

I am trying to add a record to the database with POST operation via API and I generated the attached code from Postman.
On Postman, the process goes without any problem and user registration takes place.
But when I try to generate the code as "PHP cURL" via Postman and run it on PHP, I get the relevant error.
HTTP Error 411. The request must be chunked or have a content length.
I defined POSTFIELDS as array, calculated Content-Length using strlen function and added it to Headers. But it didn't work.
My PHP code;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://xxx.xxx/xxx/xxx/xxx',
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 =>'{
"TokenKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"EmployeeList": [
{
"Example1": "1",
"Example2": "somedata2",
"Example3": "somedata3",
"Example4": "somedata4",
"Example5": "somedata5",
"Example6": "somedata6",
"Example7": "somedata7",
"Example8": "somedata8",
"Example9": "somedata9",
"Example10": "somedata10",
"Example11": "somedata11",
"Example12": "somedata12",
"Example13": "somedata13",
"Example14": "somedata14",
"Example15": "somedata15",
"Example16": "somedata16",
"Example17": "somedata17",
"Example18": "somedata18",
"Example19": "somedata19",
"Example20": "somedata20",
"Example21": "somedata21",
"Example22": "somedata22",
"Example23": "somedata23",
"Example24": "somedata24",
"Example25": "somedata25",
"Example26": "somedata26",
"Example27": "somedata27",
"Example28": "somedata28",
"Example29": "somedata29",
"Example30": "somedata30",
"Example31": "somedata31",
"Example32": null,
"Example33": null,
"Example34": null,
"Example35": "2022-12-06 22:05:22",
"Example36": false,
"Example37": true
}
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
echo "<br>";
echo $statusCode;````

how to add variable php parameters to urlencoded strings from Postman

I am testing an api on postman. The request body should be in x-www-form-url-encoded. My requests are being passed successfully, and am able to generate a snippet which I have shared here. However, some of the parameters that am adding to the body (Amount, and phone number) will not be static when the api is employed on my site. These parameters will vary by user. I have tried to define those parameters at the top of the code as you cane see $Airtime_amount and
$Recieving_mobile, but how can I pass them to the x-www-form-url-encoded CURLOPT_POSTFIELDS in the code below? See how am trying to pass them, but without success...
In other words, I have a url encoded string from postman, but the parameters in that string are static. i would like to make them dynamic..Like get user phone number from wordpress, and insert it in the urlencoded string
//Wordpress hook to call the api begins here
add_action('hrw_withdrawal_request_notification','disburse_airtime',7);
function disburse_airtime() {
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
//Snippet generated from postman begins here
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
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 => 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
You should probably build the POSTFIELDS string using the http_build_query function.
Your existing string has what looks like a JSON string for the value of the recipients parameter, so we can build that up using arrays, then encode it when we set it in the params.
function disburse_airtime()
{
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
$recipients = [
[
'phoneNumber' => $Recieving_mobile,
'amount' => $Airtime_amount
]
];
$params = [
'username' => 'sandbox',
'recipients' => json_encode($recipients)
];
$postFields = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
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 => $postFields,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
Side note, if you want to "reverse engineer" the data that is in the encoded string in order to build it up in your own code, you can do so with using urldecode and parse_str:
$str = 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D';
parse_str(urldecode($str), $params);
print_r($params);
Result:
Array
(
[username] => sandbox
[recipients] => [{"phoneNumber"=>$Recieving_mobile,"amount"=>$Airtime_amount}]
)

How to get json from instagram.com/user/?__a=1

I am using curl with postman to get a json from an Instagram user through this URL "https://www.instagram.com/kiddothegame/?__a=1".
It works and postman shows me a json response with info like:
{"seo_category_infos":[["Beauty","beauty"],["Dance & Performance","dance_and_performance"],["Fitness","fitness"],["Food & Drink","food_and_drink"],["Home & Garden","home_and_garden"],["Music","music"],["Visual Arts","visual_arts"]],.....
Now I try to do the same request with PHP:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://www.instagram.com/kiddothegame/?__a=1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And the echo result is :
string(52678) "
"
Oddly all the characters are spaces.
What am I missing?

Netsuite API throws 400 bad request

I am trying to connect with Netsuite Customer API but it throws "400 bad request"
Here is my code in PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxxxxx-sb1.suitetalk.api.netsuite.com/services/rest/record/v1/customer/1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_FAILONERROR => true,
CURLOPT_SSL_VERIFYHOST=>false,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_HTTPHEADER => array(
"Authorization: OAuth realm=\"xxxxxxx_SB1\",oauth_consumer_key=\"consumer_key_xxxxxx\",oauth_token=\"auth_token_xxxxxxxx\",oauth_version=\"1.0\""
),
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
}
var_dump($error_msg);
curl_close($curl);
var_dump($response);
?>
Here above code I copied from postman. In a postman, it returns data but when I copied code for php in a file it is not working, Could anyone suggest me where I am going to wrong ?
My localhost url : localhost/netsuite_api.php
For whatever reason, the netsuite API returns 400 even where it should be a 401 according to the docs.
From the docs about the Authentication, you need to send more fields (see page 169 following):
Authorization:
OAuth realm="12345",
oauth_consumer_key="4a3ff6c251a55057bb1e62d8dc8998a0366e88f3a8fe735265fc425368b0f154",
oauth_token="52cfe88fecf2e2b74e833e7dfc4cae79ff44c3ca9f696d61e2a7eac6c8357c3c",
oauth_nonce="qUwlmPvtGCS4sHJe8F7x",
oauth_timestamp="1462453273",
oauth_signature_method="HMAC-SHA1", oauth_version="1.0",

Categories