I am having some issues with cUrl and jsonstring.
I have a PHP script which receives a jsonstring that looks like this:
'{"CreateDateTime":"20220405T000000","ExternalID":"","HCP":"36000","PHCP":"3","Course":{"ExternalID":"7A263511-A3EB-453A-BEE9-F36ED812E51D","ClubID":"1B39F017-FC26-405C-BEC0-67AA40C9360F","Name":"Marion Golfclub","Country":"US","TeeID":"","TeeName":"Yellow","TeePar":"72","TeeRating":"712000","TeeSlope":"129"}}'
Now, when trying to run a cUrl like this:
$url = "https://my.api.io/some#mail.com/clubs/members/exchangedscorecards";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: basic ahuYty5yTyuytgFLb1JBVEVycG9Daw=="
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"CreateDateTime":"20220405T000000","ExternalID":"","HCP":"36000","PHCP":"3","Course":{"ExternalID":"7A263511-A3EB-453A-BEE9-F36ED812E51D","ClubID":"1B39F017-FC26-405C-BEC0-67AA40C9360F","Name":"Marion Golfclub","Country":"US","TeeID":"","TeeName":"Yellow","TeePar":"72","TeeRating":"712000","TeeSlope":"129"}}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
I get this error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Entities.ScorecardExchangeInput]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'CreateDateTime', line 1, position 18.
What am I doing wring, or what am I missing?
Really hoping for help and thanks in advance :-)
Related
My CSV has a single column with 40000 phone numbers
The following code read the CSV column and is fast
$dataArray = csvstring_to_array( file_get_contents('test.csv'));
My CURL code looks like this
$ch = curl_init();
$data = http_build_query($dataArray);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataArray);
curl_setopt($ch, CURLOPT_URL, "https://api.theblacklist.click/standard/api/v1/bulkLookup/key/[APIKEY]/response/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
The JSON parameters expected by the API look like the following. I am stuck about how to pass the "phones":[ and then pass the CSV Data array as a parameter to the curl/API?
curl -XGET ''
-H 'Content-Type: application/json'
-d'
{
"phones":[
"15555558353",
"15555555555",
"15555552740",
"15555552741",
"15555552738"
]
}'
It seems the API server expect JSON-formatted data to be passed as POST body
$dataArray = csvstring_to_array(file_get_contents('test.csv'));
$jsonString = json_encode(['phones' => array_values($dataArray)]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
Here is my code for trying to delete a file a file via the api
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY-ZONE-ID>/purge_cache");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = [
'X-Auth-Email: MYEMAIL',
'X-Auth-Key: MY-AUTH-KEY',
'Content-Type: application/json'
];
$data = json_encode(array("files" => "https://example.com/file/".$filename));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
The response i get is as follows
{
"success":false,
"errors":[
{
"code":1012,
"message":"Request must contain one of \"purge_everything\" or \"files\", or \"tags"
}
],
"messages":[
],
"result":null
}
Documentation says tag is optional so it should work
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"files":["http://www.example.com/css/styles.css"],"tags":["some-tag","another-tag"]}'
What am In doing wrong here ?
I maybe because your files property is not an array.
Try
$data = json_encode(array("files" => array("https://example.com/file/".$filename)));
This is a bit confusing. Post Data is usually sent in key value pairs.
Also when the post data is an array curl changes the content type to multipart/form-data
When post data is sent as a query string, it's in the format of key1=value1&key2=value2
It appears the json is the value with no key.
I would try it both as an array and string and look at the request header.
In the request header look at the content-type and the data.
To get the request header in the curl return add this option:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
You may need to add:
curl_setopt($ch, CURLOPT_POST, true);
$data[] = json_encode(array('files'=>"https://example.com/file/$filename"));
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$data = json_encode(array('files'=>"https://example.com/file/$filename"));
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
PS: You do not need to use the . concatenation when using double quotes for $filename
I want to make a post to a server I have by using php.
I was thinking in curl but all examples I find, urlfy data and I have to send a json file but not in the url.
I already have the json in an array : 'key'=>'value'...
I have to add headers, I think I can with this:
curl_setopt($ch,CURLOPT_HTTPHEADER,array('HeaderName: HeaderValue','HeaderName2: HeaderValue2'));
but I don't knoe how to add my array and post it.
Any idea?
I need to add a json like this:
[{"a":"q",
"b":"w",
"c":[{
"e":"w",
"r":"t"
}]
}]
Here how you can post the data using CURL, and as you mentioned you already have a json you can do so as
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'your api end point');
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // $postfields is the json that you have
$request_headers = array();
$request_headers[] = 'HeaderName: HeaderValue','HeaderName2: HeaderValue2';
$request_headers[] = 'Content-Type: application/json','Content-Length: ' . strlen($postfields) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$response = curl_exec($ch);
curl_close ($ch);
I can not for the life of me figure out how to keep the formatting of XML in a string, even when echoing the string of $data_string I get the XML stripped out and only the values remain.
Then I need to be able to do a CURL request via PHP with that string, I'm not sure if I have the right setup for that though.
Code:
$data_string = '<test><name>John Doe</name><age>22</age><city>seattle</age></test>';
$curl = curl_init($baseUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "xmlRequest=" . $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Length: " . strlen($data_string),
"X-DocuSign-Authentication: $header",
"Content-Type: text/xml",
"Accept: text/xml" )
);
$response = curl_exec($curl);
Test:
echo $data_string;
Result:
John Doe22seattle
You should make sure that the page you are echoing is ALSO using the MIME type text/xml.
Another way i can think of, is to escape the html characters using htmlentities()
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);