I'm trying to make a curl call using PHP. My project has a flow like this
I redirect user to the server.
User uses the services and the server redirects him to a page on my application.
I would like to make a server to server call at this point to verify certain details.
Point to be noted - I post JSON data as well as receive JSON data as response.
I have tried this. I am able to send data but don't get any response.
Is this correct?
Please help!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
As you mention in the comment:
The problem is with SSL verification of the host. It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) to setup SSL correctly, so a workaround is to add the following two options:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
Try to use the below code which works for me. and make sure the json_url is correct. When you run it on the browser you should get an output. Before try it using CURL, run in on the browser and see whether you get the output you want.
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);
Related
I am using an incoming webhook on a web app to post some data to teams. I got this working in CURL after looking up some implementation methods, but it should be noted I have no experience in CURL. The thing is, it works about half the time, then the other half this comes up in the error log:
[01-Feb-2021 09:00:59 Europe/London] Error: Could not resolve host: outlook.office.com
Was hoping someone can take a look and see if there is something obvious I am doing wrong. This is the code used in my PHP file (with the webhook ID removed for privacy)
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, 'webhook-id-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\": \"$teamsMessage\"}");
$headers = array();
$headers[] = 'Content-Type:application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
error_log("Error: " . curl_error($ch), 0);
}
curl_close($ch);
Looks like a DNS problem. Could not resolve host: outlook.office.com.
Maybe any firewall involved in blocking that? Remove CURLOPT_SSL_VERIFYPEER and CURLOPT_IPRESOLVE.
Try this simplified solution, which works well for me. I am using JSON header instead of form encoding.
// Paste the URL here
$url = 'https://outlook.office.com/webhook/XXX';
// Use the text encoded as MARKDOWN.
// Any markdown character needs to be escaped!
$body = ['text' => 'Hello World'];
$curlHandle = curl_init($url);
curl_setopt_array($curlHandle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($body)
]);
$response = curl_exec($curlHandle);
curl_close($curlHandle);
if ($response !== '1') throw new Exception('No Response from MS incoming webhook API');
I gave up after 2 days. I want to get list of all products from magento2 api in browser. First I tried "http://mydomain.con/index.php/rest/V1/products?Authorization=Bearer_TOKENHERE?searchCriteria=" but it didn’t work (still not authenticated). The next thing I’ve tried was to call api with php curl:
<?php
//test for 1 product
//API URL for authentication
$apiURL="http://mydomain.con/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "test", "password" => "123456qwe");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
$token = curl_exec($ch);
//decoding generated token and saving it in a variable
$token=json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://mydomain.con/index.php/rest/V1/products/24-MB01';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result=json_decode($result);
//printing result
print_r($requestUrl);
print_r($result);
?>
Unfortunately, this call get me a perfect answer in terminal in php storm (id, name, sku, price, etc in json) but if I want to get answer by browser it shows me Error 404 Request URL not found in server (when file is actually uploaded).
Sounds like a problem somewhere in web server. Check .htaccess in document root and host config in apache.
I'm trying to make an CURL PUT REQUEST, my code looks like:
public function assignProfile($token, $organizationId, $profileId)
{
//define enviroment and path
$host = enviroment;
$path = "/admin/organizations/".$organizationId."/users";
$data_string = '["'.$profileId.'"]';
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token.'',
'Content-Length: ' . strlen($data_string)
));
echo "<br>OK<br>";
// execute the request
$output = curl_exec($ch);
// return ID for a new case
$output = json_decode($output);
var_dump($output);
}
Each part looks correct, when I var_dump $path, $host, even $data_string looks correct. However var_dump() at the end throw just NULL
I expect I'm doing something wrong or missing something really important.
May I ask you for some advise?
Thanks
EDIT:
What i do with it:
// define
define("Audavin","here is some uniqe ID");
.
.
.
$Users = new Users;
// this return Auth token ( I verify this work with echo )
$token = $Users->authorization();
// Calling method mentioned above
$Users->assignProfile($token,"here is org id", Audavin);
I would start by making sure the URL you're making the request to actually works and returns a valid response, you can do so by using a simple REST client (like POSTMAN chrome extension for example)
If you do get a response back, try and see if it's indeed a valid JSON, if not, that could be why you're not getting anything back from json_decode (more on return values here: http://php.net/manual/en/function.json-decode.php)
Finally, It is suggested you add curl_close($ch) to the end of your code to make sure your release the curl handle.
I am trying to follow instructions from an RFC spec to retrieve data from "certificate transparency log" known logs here
Instructions say to make a JSON call but i don't seem to be having any success.. i actually don't get any data response at all.
Here is the guide I am trying to follow:
https://www.rfc-editor.org/rfc/rfc6962#section-4.6
Here is the php code I am using
<?php
$data = array("start" => "1", "end" => "10");
$data_string = json_encode($data);
$ch = curl_init('https://ct.googleapis.com/pilot/ct/v1/get-entries');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
?>
The service I was connecting to has an SSL certificate installed and was not responding to my requests. Disabling the trust verification was the solution. I added this to the CURL options CURLOPT_SSL_VERIFYPEER => false
I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:
cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=#-;type=application/json' https://my.url.here
However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:
$post = array(
"exchangeInstance" => $json_string,
"type" => "application/json",
);
$url = 'myurlhere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($post);
var_dump($result);
echo $result;
var_dump($info);
As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.
However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?
curl -F !== -d
$post = array(
"exchangeInstance" => sprintf('#%s;type=application/json', $json_string),
);