How to use array values in another array - php

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));

Related

PHP: How to send cURL request using GET?

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);

PHP curl POST for Watson Video Enrichment

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
)
);

Upload file via REST API, IPBoard

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));

Can't upload file to BOX api

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

Simple HTML Dom and Curl

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.

Categories