I want to implement a REST-Client which handles form input data and sends it to an REST Backend.
$strXml = file_get_contents($_FILES['xmlfile']['tmp_name']);
$service_url = 'api/index.php/pojects';
$curl = curl_init($service_url);
$curl_post_data = array(
"title" => $_POST['title'],
"client" => $_POST['client'],
"comment" => $_POST['comment'],
"project_number" => $_POST['project_number'],
"xml" => $strXml,
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
curl_close($curl);
var_dump($curl_response);
var_dump($strXml);
But it seems, that there's something wrong, maybe with the webserver config.
It says:
302 Found
The document has moved here.
But the var_dump of the xml string is correct. What's my error in reasoning?
You are facing a redirect.. Add this cURL param
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
Related
I am having issue while using cloudflare APIv4. Trying to update a dns record and not sure why am I receiving following error:
{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9020,"message":"Invalid DNS record type"}]}],"messages":[],"result":null}
Here is the PHP function:
function updateCloudflareDNS($zone_id,$dns_id, $updatedata){
$updatedata = '[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$zone_id."/dns_records/".$dns_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'X-Auth-Email: **********',
'X-Auth-Key: ***********'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $updatedata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Also, I am putting record type "A" correctly as mentioned on the Cloudflare API documentation
Could someone help me out with this issue?
Thanks
You're sending a payload of:
[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]
Here's what the API expects:
{"type":"A", "name":"example.com", "content":"127.0.0.1", "ttl":120, "priority":10,"proxied":false}
And this is how you properly construct JSON in PHP:
$POST = json_encode(array(
"type" => "A",
"name" => "...",
...
));
I have the following data to be posted to server via my API.
Request Body:
{
"nutrients": {
"protein": "beans",
"fats": "oil",
"carbohdrate": "starch"
}
}
Each time I run the script, am getting the following error below
{"errors":[{"status":"400","code":"031","title":"payload not parseable","detail":"Invalid formatting of the request payload."},{"status":"400","code":"026","title":"payload missing","detail":"No payload describing the resource object included in the request."}]}
It seems that it's because am not adding the variable "nutrients" in the data array to be posted.
Can someone help me out. Below is my effort so far. Thanks
<?php
$url = "https://myapi_site.com/server/";
$data = array(
'protein' => 'beans',
'fats' => 'oil',
'carbohdrate' => 'starch'
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
?>
You are posting a PHP array which hasn't been converted to JSON... you want to post the JSON itself. Here is how you can post the JSON string.
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
You can use json_encode to encode the PHP array to JSON:
http://php.net/json_encode
Define your $data variable like so:
$data = array(
'nutrients' => array(
'protein' => 'beans',
'fats' => 'oil',
'carbohdrate' => 'starch',
),
);
You can echo $data variable to see that it contains the right JSON data:
echo $data;
I have created a file on bluehost server test.php and used this file send curl request from another server ( godaddy ).
$url = 'http://dev.testserver.com/test.php';
$data_string = json_encode($fields);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS,$data_string );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
How to capture posted data on test.php and process it ?
I am trying with $_POST but its showing blank.
The question is close to this one How to send raw POST data with cURL? (PHP)
I've slightly modified your client code to follow recommendations:
<?php
$fields = ['a' => 'aaaa', 'b' => 'bbbb'];
$url = 'http://localhost/test.php';
$data_string = json_encode($fields);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, urlencode($data_string));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$curl_response = curl_exec($curl);
echo 'Response: '.$curl_response.PHP_EOL;
I do urlencode for the sent data and set headers.
The logic how to read data is explained in the question How to get body of a POST in php? I made a simple test.php file with the following code
<?php
$body = file_get_contents('php://input');
if (!empty($body)) {
$data = json_decode(urldecode($body), true);
var_export($data);
}
We read the data, decode it and parse JSON.
As one might expect the test output from the client script is the following
$ php client.php
Response: array (
'a' => 'aaaa',
'b' => 'bbbb',
)
Try to replace this, direct send array not json
curl_setopt($curl, CURLOPT_POSTFIELDS,$data_string );
With
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields );
Check this : http://php.net/manual/en/function.curl-setopt.php
I have a web service that accepts GET, POST values as parameters. So far I used to call the data using a simple CURL script, as below:
http://localhost/service/API.php?key=some_password&request=some_request&p1=v1&p2=v2
This is successfully posted using "CURLOPT_POSTFIELDS":
$base_args = array(
'key' => 'some_password',
'request' => 'some_request',
);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
The problem is, I need to also POST a JSON body to this service. I found out that this can be done by using "CURLOPT_POSTFIELDS", but also found out that I am not able to use both POST and JSON.
$data_string ="{'test':'this is a json payload'}";
$ch = curl_init('http://localhost/service/API.php');
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)));
$result = curl_exec($ch);
echo ($result); // string returned by the service.
I can either set the POST values, OR set the JSON body, but not both. Is there a way I can push the two different types of values?
Try sending the json text via a variable:
$data = array('data' => "{'test':'this is a json payload'}", 'key' => 'some_password', 'request' => 'some_request');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
Then in your API you do:
var_dump(json_decode($_POST['data']));
You can still use the other key/request variables.
I could not find any way of sending both POST and JSON body at the same time. Therefore the workaround was to add the POST arguments as part of the JSON body.
$base_args = array(
'key' => '8f752Dd5sRF4p',
'request' => 'RideDetails',
'data' => '{\'test\',\'test\'}',
);
$ch = curl_init('http://localhost/service/API.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($base_args));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen(http_build_query($base_args)))
);
$result = curl_exec($ch);
echo ($result);
The full JSON can be returned by the API as below:
var_dump(json_decode(file_get_contents('php://input')));
I am able to send the GET request and receive the response at following line.
$curl_resp = curl_exec($curl);
I used the following to parse the response, but it does not work, I have manually set some values to $curl_resp but still not sure how to access the value of each tag of the xml separately.
$xml = simplexml_load_string($curl_resp);
NOTE: I recevice the actual xml but cant parse it, (I need to get each tag's value separately in a variable)
Code:
<?php
$service_url = ' The Url goes here';
$curl = curl_init($service_url);
$curl_post_data = array(
"PASSWORD" => 'pass',
"USERNAME" => 'username'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_resp = curl_exec($curl);
curl_close($curl);
Your variable $curl_response is different than $curl_resp (what you're trying to parse)
You can access the value of each tag just like any other array.
if the CURLOPT_RETURNTRANSFER option is not set then your $curl_resp will just return true/false.
if it is set you may be returning false or a poorly formed xml string. If you post more code or the actual curl response text we may be able to provide more info.
EDIT:
upon reading the code looks like you are assigning the response text to $curl_response instead of $curl_resp
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "your url");
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);