I want to send post request as php stream
$aruguments = http_build_query(
array(
'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'appid' => 730,
'min' => 20,
'items_per_page' => 100
)
);
$opts_stream = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/json' .
'x-requested-with: XMLHttpRequest',
'content' => $aruguments
)
);
$context_stream = stream_context_create($opts_stream);
$json_stream = file_get_contents('https://api.example.de/Search', false, $context_stream);
$data_stream = json_decode($json_stream, TRUE);
For some reason i get error saying:
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
If i send this same request with cUrl it works normaly but its very slow.
Here is my cUrl request that works
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.example.de/Search');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"apikey\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"min\": 20, \"appid\": 730, \"items_per_page\": $number_of_items_per_request }");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Requested-With: XMLHttpRequest';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close ($ch);
There are a couple of issues with the posted code.
Headers
When you're adding headers, you set them all in one single string. For the target server to know when one header ends and the other begins, you need to separate them using new lines (\r\n):
'header' => "Content-Type: application/json\r\n"
. "x-requested-with: XMLHttpRequest\r\n",
Post data
The big difference between your stream context and your cURL code is that your cURL code are posting the data in json-format, while you're stream context are posting the data as a x-www-form-urlencoded string. You're still telling the server that the content is json though, so I guess the server gets a bit confused.
Post the data as json instead by changing:
$aruguments = http_build_query(
array(
'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'appid' => 730,
'min' => 20,
'items_per_page' => 100
)
);
to
$aruguments = json_encode(
array(
'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'appid' => 730,
'min' => 20,
'items_per_page' => 100
)
);
Related
Please I need your help. I need to make a post request using file get contents and I am getting this error
file_get_contents(https://api.paystack.co/transferrecipient): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
Here's the company's cURL requirement:
curl -X POST -H "Authorization: Bearer SECRET_KEY" -H "Content-Type: application/json" -d '{
"type": "nuban",
"name": "Account 1029",
"description": "Customer1029 bank account",
"account_number": "01000000010",
"bank_code": "044",
"currency": "NGN",
}' "https://api.paystack,co/transferrecipient
AND MY CODE
//create the content to post to get recipient code
$content = json_encode([
'type' => 'nuban',
'name' => $request->username,
'description' => $request->username.' bank account',
'account_number' => $request->acc_number,
'bank_code' => $request->bank,
'currency' => 'NGN'
]);
$opt = [
"http" => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'header' => 'Authorization: Bearer secret_key',
$content
]
];
$con = stream_context_create($opt);
$datas = file_get_contents('https://api.paystack.co/transferrecipient', false, $con);
return $datas = json_decode($datas, true);
You have two header keys and no content key for your json payload. You should be able to change the header to an array and set the content key as follows:
$opt = array(
'http' => array(
'method' => 'POST',
'header' => array(
"Content-Type: application/json",
"Authorization: Bearer secret_key"
),
'content' => $content
)
);
I think this will be easier with php-curl:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.paystack.co/transferrecipient");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'header' => 'Authorization: Bearer secret_key'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
I am working with synapse and having issue while uploading user doc attachment, actually same thing is working on our live server but not working with test server in sandbox mode
If any one can help :)
Reference URL: https://docs.synapsepay.com/v3/docs/attach-document
API URL: https://sandbox.synapsepay.com/api/3/user/doc/attachments/add
Response: Array ( [error] => Array ( [en] => None is not of type 'string' ) [error_code] => 200 [http_code] => 400 [success] => )
Code as Below:
$url = "https://sandbox.synapsepay.com/api/v3/user/doc/attachments/add";
// KYC Documentation
$payload = array(
"login" => array(
//Oauth_key of the user to add KYC doc
"oauth_key" => "hTUCH4kO89qGZDpyEdoq55ODYugwwRsd57ti8ohZ"
),
"user" => array(
//doc data
"doc" => array(
'attachment' => 'data:text/csv;base64,SUQsTmFtZSxUb3RhbCAoaW4gJCksRmVlIChpbiAkKSxOb3RlLFRyYW5zYWN0aW9uIFR5cGUsRGF0ZSxTdGF0dXMNCjUxMTksW0RlbW9dIEJlbHogRW50ZXJwcmlzZXMsLTAuMTAsMC4wMCwsQmFuayBBY2NvdW50LDE0MzMxNjMwNTEsU2V0dGxlZA0KNTExOCxbRGVtb10gQmVseiBFbnRlcnByaXNlcywtMS4wMCwwLjAwLCxCYW5rIEFjY291bnQsMTQzMzE2MjkxOSxTZXR0bGVkDQo1MTE3LFtEZW1vXSBCZWx6IEVudGVycHJpc2VzLC0xLjAwLDAuMDAsLEJhbmsgQWNjb3VudCwxNDMzMTYyODI4LFNldHRsZWQNCjUxMTYsW0RlbW9dIEJlbHogRW50ZXJwcmlzZXMsLTEuMDAsMC4wMCwsQmFuayBBY2NvdW50LDE0MzMxNjI2MzQsU2V0dGxlZA0KNTExNSxbRGVtb10gQmVseiBFbnRlcnByaXNlcywtMS4wMCwwLjAwLCxCYW5rIEFjY291bnQsMTQzMzE2MjQ5OCxTZXR0bGVkDQo0ODk1LFtEZW1vXSBMRURJQyBBY2NvdW50LC03LjAwLDAuMDAsLEJhbmsgQWNjb3VudCwxNDMyMjUwNTYyLFNldHRsZWQNCjQ4MTIsS2FyZW4gUGF1bCwtMC4xMCwwLjAwLCxCYW5rIEFjY291bnQsMTQzMTk5NDAzNixTZXR0bGVkDQo0NzgwLFNhbmthZXQgUGF0aGFrLC0wLjEwLDAuMDAsLEJhbmsgQWNjb3VudCwxNDMxODQ5NDgxLFNldHRsZWQNCjQzMTUsU2Fua2FldCBQYXRoYWssLTAuMTAsMC4wMCwsQmFuayB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 'POST',
'content' => json_encode( $payload ),
'header' => "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$data_string = json_encode($payload);
curlIT($url, $data_string);
function curlIT($url, $fields){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json')
);
$result = curl_exec($ch);
print_r($result);
}
exit;
?>
Synapse api changed you need to send auth key and client id in headers
Take a look of new document
https://docs.synapsepay.com/docs/adding-documents
Good morning all,
I'm trying to create a lead entity in Microsoft Dynamics NAV 365, from a php CURL script. However I keep getting a "HTTP Error 401 - Unauthorised: Access is denied" in my CURL response. I can however, create a lead via the web interface fine.
I've created my object from the lead entity type as described on the MSDN docs website.
Below is my code:
$lead = array('person' =>
array(
'topic' => 'WEB LEAD',
'name' => $fullname,
'firstname' => $firstname,
'lastname' => $lastname,
'companyname' => $company,
'telephone1' => $telephone,
'emailaddress1' => $email,
'description' => $comment,
),
);
$dynamics = $url . '/api/data/v8.2/leads';
$ch = curl_init($dynamics);
$options = array(
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json; charset=utf-8',
'OData-MaxVersion: 4.0',
'OData-Version: 4.0',
'Accept: application/json',
),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_USERPWD, 'username:password',
CURLOPT_POSTFIELDS => json_encode($lead),
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
You need previously get authorization token from AAD
After you get all token you need to add the authorization in the request http header
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json; charset=utf-8',
'OData-MaxVersion: 4.0',
'OData-Version: 4.0',
'Accept: application/json',
'Authorization: <put the token here completly with name>',
),
Adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); solved the problem for me. The whole source looks like this:
$ch = curl_init();
if(!empty($parameters)){ //Add params (array) to your request if you want
$url .= "?".http_build_query($parameters);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
$login = sprintf('%s:%s', self::USERNAME, self::PASSWORD);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Connection: Keep-Alive',
'Accept: application/json',
'Content-Type: application/json; charset=utf-8'
)
);
$response = curl_exec($ch);
$response_info = curl_getinfo($ch);
curl_close($ch);
I'm trying to figure out the paypal API, and I have the following code, which should make a call, get an access token, and then make the API call. The first part works(up until the $accesstoken line), and returns the access token properly, but the second part doesn't return anything. The code this is supposed to mimic can be found here: Make Your First Call
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$headers = array(
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, $clientID . ':' . $clientSecret);
$curl = curl_exec($ch);
$x = json_decode($curl, TRUE);
print_r($x);
$accesstoken = $x['access_token'];
$headers2 = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer' . $accesstoken
);
$data = array(
"intent" => "sale",
"redirect_urls" => array(
"return_url" => "http://example.com/your_redirect_url/",
"cancel_url" => "http://example.com/your_cancel_url/"
),
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(
"transactions" => array(
"total" => ".99",
"currency" => "USD"
)
)
);
$saleurl = "https://api.sandbox.paypal.com/v1/payments/payment";
$sale = curl_init();
curl_setopt($sale, CURLOPT_URL, $saleurl);
curl_setopt($sale, CURLOPT_VERBOSE, TRUE);
curl_setopt($sale, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($sale, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($sale, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($sale, CURLOPT_HTTPHEADER, $headers2);
$finalsale = curl_exec($sale);
$verb = json_decode($finalsale, TRUE);
print_r($verb);
Curl doesn't make complete sense to me, any help would be appreciated.
UPDATE:
I changed the format of the headers to:
$headers2 = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $accesstoken
);
as per one of the answers. Now it is displaying:
[name] => MALFORMED_REQUEST
[message] => Incoming JSON request does not map to API request
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST
[debug_id] => f53a882702a04
You are not setting your headers correctly ...
$headers = array(
'Accept: application/json',
'Accept-Language: en_US'
);
and
$headers2 = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $accesstoken
);
Is the correct format.
Also note the (space) after Bearer inbetween your $accesstoken
Edit: Update for your JSON ( i think this is right but echo it out and check it against the reference, I might have one to many array()
$data = array(
"intent" => "sale",
"redirect_urls" => array(
"return_url" => "http://example.com/your_redirect_url/",
"cancel_url" => "http://example.com/your_cancel_url/"
),
"payer" => array(
"payment_method" => "paypal"
),
"transactions" => array(array(
"amount" => array(
"total" => ".99",
"currency" => "USD"
)
)
)
);
You need a space here
$headers2 = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accesstoken // Added a space after Bearer
);
See if it works now
Change
curl_setopt($sale, CURLOPT_POSTFIELDS, json_encode($data));
to
curl_setopt($sale, CURLOPT_POSTFIELDS, $data);
The json encode function breaks it
file_get_contents not working for fetching fata from facebook using batch requests.Am using the code below:
$url='https://graph.facebook.com/?batch=[{ "method": "POST", "relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]& access_token=xxxxxxx&method=post';
echo $post = file_get_contents($url,true);
it produces
Warning: file_get_contents(graph.facebook.com/?batch=[{ "method": "POST", "relative_url": "method/fql.query?query=SELECT+first_name+from+user+where+uid=12345"}]&access_to ken=xxxx&method=post): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/user/workspace/fslo/test.php on line 9
I would say the most likely answer to this is that you need to pass the URL values through urlencode() - particularly the JSON string.
Also, you should be POSTing the data.
Try this code:
NB: I presume you are building the URL from several variables. If you edit the question with your actual code, I will provide a solution using that code
<?php
$baseURL = 'https://graph.facebook.com/';
$requestFields = array (
'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]',
'access_token' => 'whatever'
);
$requestBody = http_build_query($requestFields);
$opts = array(
'http'=>array(
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n"
. "Content-Length: ".strlen($requestBody)."\r\n"
. "Connection: close\r\n",
'content' => $requestBody
)
);
$context = stream_context_create($opts);
$result = file_get_contents($baseURL, FALSE, $context);
A "more standard" way to do this these days is with cURL:
<?php
$baseURL = 'https://graph.facebook.com/';
$requestFields = array (
'batch' => '[{"method":"POST","relative_url":"method/fql.query?query=SELECT+first_name+from+user+where+uid=12345678"}]',
'access_token' => 'whatever'
);
$requestBody = http_build_query($requestFields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseURL);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($requestBody),
'Connection: close'
));
$post = curl_exec($ch);