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);
Related
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));
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));
I wrote this code. I send data to $url = "https://upload.box.com/api/2.0/files/content", but i don't get a response. Maybe someone has also had this problem?
$absolutePath = 'home/my/path/uploads/media/ClientPDF/0001/01/c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf';
$json = json_encode([
'name' => 'c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf,
'parent' => ['id' => 7479666489] //parent folder
]);
$fields = [
'attributes' => $json,
'file' => #$absolutePath
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer MY_TOKEN_KEY'
'Content-Type:multipart/form-data'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($ch);
var_dump(json_decode($response, true)); die;
var_dump - print nothing, i can't debug this request. Please, help me to solve this problem
Hi is curl the best method for using POST with cookie and also navigated to another page for scrapping? I'm using the coding below and I can't get it to work.
include('simple_html_dom.php');
$data = array(
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => '%2FwEPDwUKLTcyODA2ODEwMGRk',
'Myname' => 'justdev12345',
'Mypassword' => '12345',
'idLogin' => 'Login',
'tmplang2' => '6',
'fm' => '',
'jc' => '',
'LoginRef' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.site.com/mainframe.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = new simple_html_dom();
$output = file_get_html('http://profiles.site.com/profile_900.aspx?AccountID=ShopCartUpdate');
print $output;
Your code is fine up until the end where you throw away the curl response and load the url with simple-html-dom. If you want to use the curl response it should look like this:
$html = str_get_html($output);
$html->find('title');
Otherwise you should probably remove all the curl code.
My Facebook app publishes a story to the user wall with an http post:
$args = array('access_token' => $ACCESS_TOKEN,
'message' => 'testing message',
'picture' => $appin_logo,
'link' => $appin_canvas_url,
'name' => $appin_name,
'caption' => $post_score,
'description' => $post_rfs,
);
$ch = curl_init(); $url = 'https://graph.facebook.com/me/feed'; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $args); $data = curl_exec($ch); curl_close($ch);
That works all fine, except for one thing: $post_rfs is an array. I'd like to output its values in a neat way, with a comma after every value i.e.. What should I do?
Thanks in advance.
Try this:
implode(', ', array_values($post_rfs));