I am trying to create a new list via the mailchimp api, I have tried tweaking the code so much but keep getting the error:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"0174d737-b3d4-40a6-9cd4-e934ed8578a7","errors":[{"
field":"","message":"Schema describes object, NULL found instead"}]}
My code (api key is valid):
$data = array( // the information for your new list--not all is required
"name" => $name,
"contact" => array (
"company" => $company,
"address1" => $address1
),
"permission_reminder" => $permission_reminder,
"use_archive_bar" => $archive_bars,
"campaign_defaults" => array(
"from_name" => $from_name,
"from_email" => $from_email,
"subject" => $subject,
"language" => $language
),
"notify_on_subscribe" => $notify_subs,
"notify_on_unsubscribe" => $notify_unsubs,
"email_type_option" => $type,
"visibility" => $visibility
);
$ch = curl_init("https://$dataCenter.api.mailchimp.com/3.0/lists/");
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($result); // display API response
I'm not seeing any JSON encoding of your payload in the code you provided. You will need to json_encode() your payload ($data) in order for MailChimp to be able to read it.
Like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Related
I am using cURL for an API call, below is my code:
$data = array(
'r' => 'create',
'_object' => 'Question',
'_api_key' => '........',
'_user_id' => $creator_id,
'_subtype_id' => $type_id,
'_subtopic_id' => $subtopic_id,
'_title' => $title,
'_description' => $description,
'_encoded_xml' => $main_xml
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://urlhere/v1/resources/objects");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);
I want these parameters to be sent as GET request instead of POST, how can I do that pls advise
CURLOPT_POSTFIELDS is for the body (payload) of a POST request. For GET requests, the payload is part of the URL.
You just need to construct the URL with the arguments you need to send (if any), and remove the other options to cURL.
$data = array(
'r' => 'create',
'_object' => 'Question',
'_api_key' => '........',
'_user_id' => $creator_id,
'_subtype_id' => $type_id,
'_subtopic_id' => $subtopic_id,
'_title' => $title,
'_description' => $description,
'_encoded_xml' => $main_xml
);
$send_data = http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://urlhere/v1/resources/objects?" . $send_data);
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);
Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);
Im trying to upload files via IPBoards REST Api but dont really know how to format the files.
This is how it looks right now but only gets error:
{
"errorCode": "1L296\/B",
"errorMessage": "NO_FILES"
}
Code:
$post = array(
'category' => 1,
'author' => 1,
'title' => 'Test title',
'description' => 'test description',
'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}",
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);
Here is the api documentation: API doc
Try using the http_build_query method and change the $post array slightly:
'files' => array($filename => $contents),
And change to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.
We are attempting to post to a rest API endpoint that requires a "POST object" within one of the keys. In javascript/jquery, this works fine. But, using CURL in PHP the endpoint doesn't receive the object (called "components") here:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'http://api.sitesitesite.com');
$comps = array('slug' => "xyz",
'visble' => 1,
'color' => "xyz",
'shape' => "xyz",
'version' => "2",
);
$post_args = array();
$post_args['components'] = $comps;
$post_args['id'] = $id;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Get rid of "CURLOPT_CUSTOMREQUEST" and use "CURLOPT_POST"
curl_setopt($ch, CURLOPT_POST, true);