Requesting a cURL GET Request in PHP outputs garbage - php

I'm trying to build a Connector to Dynamics 365 Business Central but I'm having problems getting the data. Please help me figure out why when I send a GET request using cURL and PHP, it produces the following output:
�S[O�0�+(��&��qB��i�t�F/���=�ا��Ďb�}N�"&�M�����������8�0% �;
Here is my code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.businesscentral.dynamics.com/{tenantID}/customers/?$filter=displayName%20eq%20'Shawn%20Test'",
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_HTTPHEADER => array(
"Authorization: {Auth Code}"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

It seems like a character encoding issue, or maybe you are receiving binary data.
What is this endpoint supposed to return? Check the response headers, it might give you a clue.

This is an encoding problem, try to remove CURLOPT_ENCODING line from your request or try to choose different encoding from your browser

Related

Problem to get Data with CURL from an API "*.glitch.me" Domain

I am trying to get the JSON data from a Website called
"https://thesimpsonsquoteapi.glitch.me/"
It has some sort of protection and take a while to open.
The Problem. After the site is loaded, you get access to some API calls like
"https://thesimpsonsquoteapi.glitch.me/quotes"
If I try it in Postman, I get back the right JSON response also if I open it up in the browser.
But if I try the to load the Data via curl in PHP i get the html code back from the first protection side. What can I do to get the JSON like in Postman.
$curl = curl_init();
$url = 'https://thesimpsonsquoteapi.glitch.me/quotes?count=1';
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 => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
thx for any help

Trouble with Param arguments for an Api

Im trying to get all cities of a specific country from an API(https://countriesnow.space/). Im using Php and curl but the result says:
{
"error": true,
"msg": "missing param (country)"
}
Im using a script which is given by but it does not work: https://documenter.getpostman.com/view/1134062/T1LJjU52#4829d16f-0f4e-43ec-886e-68ebad1221d8
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://countriesnow.space/api/v0.1/countries/cities',
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 =>'{
"country": "nigeria"
}',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Could anyone tell me whats wrong here? Really cant tell whats the issue.
cURL will automatically set the Content-Type to application/x-www-form-urlencoded, when you pass a string value for CURLOPT_POSTFIELDS.
But this is not supposed to be form data, but JSON. Add
CURLOPT_HTTPHEADER => ['Content-Type: application/json']
to your cURL options, to let the API know you are in fact sending JSON.

WordPress JSON API AUTH with PHP Curl

I am working with wordpress and using this plugin https://wordpress.org/plugins/json-api-auth/ for JSON API Auth and I am trying to return success with curl in PHP but whenever I run the code I am getting {"status":"error","error":"Invalid username and\/or password."}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"',
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;
?>
But when I put this in the browser url https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd I am getting success
{"status":"ok","cookie":"xyz","cookie_name":"xyzxyz","user":{"id":1,"username":"abcd","nicename":"abcd","email":"abcd#abcd.com","url":"","registered":"2020-09-10 10:42:41","displayname":"abcd","firstname":"abcd","lastname":"abcd","nickname":"abcd","description":"","capabilities":{"administrator":true},"avatar":null}}
Can you help me what I am doing wrong with curl in PHP?
Original version of CURLOPT_URL that I have used in code
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="'.$this->input->post('email').'"&password="'.$this->input->post('password').'"',
Remove the " from the cURL url query string. The WordPress PHP handling your request would read your username as "abcd" instead of abcd as you are trying to achieve.
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username="abcd"&password="abcd"'
Should be
CURLOPT_URL => 'https://example.com/api/auth/generate_auth_cookie/?username=abcd&password=abcd'

CRM show data using PHP CURL

I want to show my data on my CRM using PHP with curl and also using API.
If there is any other method. You can kindly show it to me
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://b.maxmind.ma/api/leads",
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_HTTPHEADER => array(
"authtoken: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyfdfdiOjE1ODEwODk2MjJ9.Selc36cmT3XyXH6cGdJ3SN-332kx7"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
the response show : error code: 1020
I think the problem is that the website you are trying to request is protected by Cloudflare.
When i try by the postman , this is the header response a get :
The error code 1020 you got is from cloudflare .
First, you can try using this solution that bypasses the Cloudflare problem
KyranRana/cloudflare-bypass
If not working, in this case, you must contact the site owner to remove the block (more here)

AtTask API 'PUT' CURL Fails to Update

I have been working on some code to automate an update to a specific task in AtTask using the API. I am finding some problems using PHP's CURL request for a PUT command. Here is a copy of the code:
$updateURL = "https://[COMPANYURL].attask-ondemand.com/attask/api/v4.0/task?updates={'ID':'TASKID','name':'Ok. Here we go again','commitDate':'2015-10-27T17:30'}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $updateURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"sessionid: " . $this->sessionId
),
));
$attask_update_json = curl_exec($curl);
The code executes with out any errors being reported, but the return response is blank. I have verified my URL using postman and it processes correctly, but I can not get the code to process in PHP. Any help would be appreciated.

Categories