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);
Related
I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.
Terminal curl request :
curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"
Response :
{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}
PHP Code :
$zohouser = '6000';
$access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
$cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $cURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Zoho-oauthtoken " . $access_token,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = json_decode(curl_exec($curl));
$err = curl_error($curl);
print_r($err);
curl_close($curl);
print_r($response);exit;
Not getting any response or error during run this php curl request.
Can you please help me how to pass string as param in php curl request.
$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";
$arrHeader= array(
'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
);
$params= array(
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"
);
$ch= curl_init();
curl_setopt_array($ch,array(
CURLOPT_URL =>$strURL,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $arrHeader,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 0,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$strResponse= curl_exec($ch);
print_r($strResponse);
echo curl_error($ch);
Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");
$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
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
)
);
I want to make a simple post request to an API server. I know, there are lot of information about cURL in web, but I can't figure out, why am I getting:
[message] => Authorization has been denied for this request.
So the API uses standard bearer token(in the header Authorization: Bearer {tokenID})
my php code:
$body = array(
"id" => $uuid,
"userAccountId" => $userAccountId,
"email" => $email,
"first_name" => $first_name,
"last_name" => $last_name,
"phone" => $phone,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, http_build_query(array(
'Authorization' => $token,
)));
curl_setopt($ch, CURLOPT_URL, $myurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
The most interesting and weird thing is, that, when I send a Post request to the same url with the same body and the same header(same token) using postman, it works, however it doesn't work with php cURL. Also I tried with https://www.codepunker.com/tools/http-requests and got the same Authorization error. Has anyone any ide what could it be ?
I found the issue: I should have used json_encode($body) instead http_build_query($body) and the header in the following way:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer .....',
'accept: application/json',
'content-type: application/json',
));
I am trying to cURL apptweak (ref - https://apptweak.io/api )
curl -H 'X-Apptweak-Key: your-api-key' https://api.apptweak.com/ios/applications/284882215.json
I have my key and can curl from the terminal. In PHP, I get "authorization token missing".
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/ios/applications/284882215.json&country=US&language=en',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
X-Apptweak-Key => 'MY-KEY-IS-HERE'
)
));
$resp = curl_exec($curl);
print $resp;
curl_close($curl);
Is X-Apptweak-Key => 'MY-KEY-IS-HERE' being a POST field the issue here?
What is wrong?
you can add X-Apptweak-Key between single quotes it's a key
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => 'MY-KEY-IS-HERE'
)
or you can try this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic MY-KEY-IS-HERE'));
or you can use:
curl_setopt($ch, CURLOPT_USERPWD, "X-Apptweak-Key:MY-KEY-IS-HERE");
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