I have been trying, for days, to send a simple XML request to an API. Even with the help of the tech support, absolutely nothing works. I still get an error telling me that my XML isn't well-formed or is invalid.
Here is my cURL request:
$data = array_merge([
'ssl_transaction_type' => "$transactionType",
'ssl_merchant_id' => $this->merchant_id,
'ssl_user_id' => $this->user_id,
'ssl_pin' => $this->pin
], $data);
$xml = new \SimpleXMLElement('<txn/>');
$data = array_flip($data);
array_walk_recursive($data, array ($xml, 'addChild'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getXMLUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("xmldata=" . explode(PHP_EOL, $xml->asXML())[1])));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
Why do I explode the XML? It's because I need to only get the part with the root, not the version, etc. according to the tech support.
I followed this: XML request is not well-formed or request is incomplete but it still doesn't work.
Related
I'm trying to use the Stability.ai API to generate Stable Diffusion AI images.
I got my API key from https://beta.dreamstudio.ai/membership?tab=apiKeys
I am following the textToImage docs here: https://api.stability.ai/docs#tag/v1alphageneration/operation/v1alpha/generation#textToImage
I'm trying to use PHP / cURL to generate an Image using the API
$url = 'https://api.stability.ai/v1alpha/generation/stable-diffusion-512-v2-0/text-to-image';
$data = array(
"api_key_header_Authorization" => "sk-XXX"); // API key here
$data['text_prompts']['text'] = 'a happy robot';
$data['text_prompts']['weight'] = 1;
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
Here is the response:
{"name":"decode_payload","id":"sDzSQ7y2","message":"json: cannot unmarshal object into Go struct field TextToImageRequestBody.text_prompts of type []*server.TextPromptRequestBody","temporary":false,"timeout":false,"fault":false}
I was hoping to get a success response.
text_prompts must be an array of objects.
$data['text_prompts'][0]['text'] = 'a happy robot';
$data['text_prompts'][0]['weight'] = 1;
See here:
https://api.stability.ai/docs#tag/v1alphageneration/operation/v1alpha/generation#textToImage (The Box on the right side)
As Foobar mentioned, I needed an array of objects.
Now I have:
$url = 'https://api.stability.ai/v1alpha/generation/stable-diffusion-512-v2-0/text-to-image';
$headers = [
'Authorization: sk-XXX', // API key
'Accept: image/png',
];
$data['text_prompts'] = $arr_of_obj = array(
(object) [
'text' => 'a happy robot',
'weight' => 1
]);
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
which returns the png data of generated AI image
I'm taking multiple documents of candidate from database, put it into one array and sending this array and some other additional information to the REST API. Then it's throwing an exception FILE Type NOT Support. When sending single image it works properly.
foreach($a_files as $a_file)
{
$file_name_with_full_path=$a_file->uploaded_path.$a_file->document_name;
$mime_type=mime_content_type($file_name_with_full_path);
$cFile[] = '#'.realpath($file_name_with_full_path).';type='.$mime_type.';';
}
Sending this information through REST API. My post data is like:
$post= array('level' => "level",
'nameOfInstitute' => "nameOfInstitute",
'nameAsPerDocument' => "nameAsPerDocument",
'issueDate' => "issueDate",
'registrationNumber' => "registrationNumber",
'yearOfPassing' =>"yearofpassing",
'degree' =>"degree",
'fieldOfStudy' =>"fieldofstudy",
'grade' =>"grade",
'nameOfBoardUniversity'=>"university",
'file' =>$cFile
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_USERPWD, $val);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec ($ch);
$err = curl_error($ch);
curl_close($ch);
It's throwing an exception file type not supported. But when sending single file, it's working fine (without loop). What is the problem in above code when sending multiple files in the API. Please help me out from this problem.
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" => "...",
...
));
When i do a curl call using php which is as shown below
<?php
$username = "XXXX";
$password = "XXXX";
$url = "https://domainname/method";
$ch = curl_init();
$fullAddress="202 220 GEORGE ST";
$payload = json_encode( array( "payload"=>[array("fullAddress"=>$fullAddress)],
"sourceOfTruth"=>'AUPAF'));
//var_dump($payload);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# Send request.
$result = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
echo '<pre>', htmlentities($result), '</pre>';
$json = json_decode($result, true);
?>
it outputs an weird response saying that
<html><head><title>Apache Tomcat/7.0.52 (Ubuntu) - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:whi
I'm actually stuck up with it for several hours so any suggestions would be helpfull
I've post a comment, but I recon that it's not very well readable.
Here is my suggestion, to use for nested arrays:
json_encode( [ 'payload' => [
['fullAddress' => $fullAddress]
],
'sourceOfTruth' => 'AUPAF'
] );
In your example your arrays where further nested, maybe the server does not expact this.
A few other tips:
Make sure the URL to the API is correct.
The headers send to the server are as expected.
the credentials you entered, are they right? might this be a permission issue?
Shouldn't the password be base64_encode'd?
Also try just printing all content, and accept text/html for a bit. Just to see what the error message might be.
I am trying to do a free/busy query on a Google Resource Calendar (Google Apps). The API documentation for Freebusy query gives these directions about the request:
HTTP Request:
POST https://www.googleapis.com/calendar/v3/freeBusy
Request Body Structure:
{ "timeMin": {datetime},
"timeMax": {datetime},
"items": [
{"id": {string}
}
]
}
Here's my attempt, using curl:
$url = "https://www.googleapis.com/calendar/v3/freeBusy";
global $headers; // holds ClientLogin authentification
$freebusy_post = array(
"timeMin" => '2012-12-31T00:00:00',
"timeMax" => '2013-01-08T23:00:00',
"timeZone" => 'America/New York',
/*** "items" below commented out because posting a multidimensional array returns an
"Array to string conversion" error. However, I think the API requires
"items" to be an array. ***/
//"items" => array(
// "id" => "https://www.googleapis.com/calendar/v3/calendars/gdev.wellesley.edu_313736333535343332#resource.calendar.google.com"
// )
/*** My attempt at using a plain string instead of an array ***/
"items" => "gdev.wellesley.edu_313736333535343332#resource.calendar.google.com",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $freebusy_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
print_r($data);
My problem: $data doesn't print anything.
I'm brand new to PHP, curl, and the Google Apps Calendar API. I'd love any help/ lessons/ advice you can offer!
Not sure, but you have probably to convert the php array to a json first.
$freebusyjson = json_encode($freebusy_post);
i started with your code and modified it and the following worked for me...
Note how i formatted the calendar ID in the item variable. I also only needed to attach an API key to the URL as it is a public calendar. I don't think I needed it initially, but then I reached the number of requests that could be made without using an API key.
Finally, note the format of the Time/Date. I'm saying this is the only way to do it, but it worked for me.
$url = "https://www.googleapis.com/calendar/v3/freeBusy/?key=MY_API_KEY";
$freebusy_post = array(
"timeMin" => '2016-11-01T00:00:00.000Z',
"timeMax" => '2016-12-21T00:00:00.000Z',
"timeZone" => ' America/New York',
"items" => array(array('id'=>"mywebsite.com_randomlettershere#group.calendar.google.com")),
);
$freebusy_post = json_encode($freebusy_post);
$ch = curl_init();curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $freebusy_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
print_r($data);