Teaching myself some Facebook feed posting via PHP and I'm trying to figure out how I would execute this curl HTTP POST via the the PHP curl command, any suggestions?
curl -X POST \
-F 'message=Post%20with%20app%20access%20token' \
-F 'access_token=YOUR_APP_ACCESS_TOKEN' \
https://graph.facebook.com/4804827/feed
It's found here
This is how to do it:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/4804827/feed");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'message' => 'Post with app access token',
'access_token' => 'YOUR_APP_ACCESS_TOKEN'
));
echo curl_exec($ch);
curl_close($ch);
I recommend looking at the documentation:
http://www.php.net/manual/en/function.curl-init.php
http://www.php.net/manual/en/function.curl-setopt.php
Related
I`m using weasyprint by aquavitae for generating PDFs for my Laravel application via cURL
curl -X POST \
-H Content-Type:text/html \
-T ./test.html http://127.0.0.1:5001/pdf?filename=test.pdf \
-o test.pdf
This is an example of cURL request to a weasyprint which gives me a good responce, but when u try to do this in my PHP:
$path_to_file = 'd:/index.html';
$ch = curl_init();
$headers = array(
"Content-Type:text/html"
);
curl_setopt($ch, CURLOPT_URL, 'http://192.168.99.100:5001/pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'datafile' => curl_file_create($path_to_file , mime_content_type($path_to_file), basename($path_to_file))
]
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
I get my responce as a line and when I want to responce it to a browser or into a file I get some information about HTML file I sent:
PDF file I get with PHP cURL
tho my HTML file contains only
<h1>hello world</h1>
I can see something like that when I use Postman too.
I think that there`s some difference beetwen how I send file with CMD cURL and PHP cURL.
Do you guys have any ideas?
I have a curl request that was given to me as part of an API documentation, and can't for the life of me get it to work via php (in Laravel).
The documentation says to do this...
wget -O- \
--header 'x-user:x' \
--header 'x-password:y' \
'http://example.com'
What I'm trying is this...
$url = 'http://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "test_user:test_password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
dd($info);
This is just echoing out Authentication Failed and then a bunch of info on my request, but not actual data.
Can anyone point out what I'm doing wrong?
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'x-user: something', 'x-password: something' ));
I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST
Same thing I have tried with php But it is giving the following error
{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}
The following one I have tried
$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'name'=>'NEW USER',
'is_platform_access_only'=>'true',
));
//Adding Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$developer_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
If I remove the Post parameters, and run with only headers it is give the result of users. But with post it is throws error.
I have rise the same question in Perl tag with Perl code. There I got answer by user #melpomene.
We should encode the data as JSON. It is working,
Then the final code is
$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);
I was trying to work on cloudflare api and I have found this in their documentation. Unfortunately I have no Idea how I can use this in fsockopen or through curl. Here is the example of POST Request. I just need to know how I can make a request with below data as POST using curl or fsockopen
curl https://www.cloudflare.com/api_json.html
-d 'a=cache_lvl' \
-d 'tkn=8afbe6dea02407989af4dd4c97bb6e25' \
-d 'email=sample#example.com' \
-d 'z=example.com' \
-d 'v=agg'
Here is the link to cloudflare
https://www.cloudflare.com/docs/client-api.html#s4.2
Try this one:
$ch = curl_init("https://www.cloudflare.com/api_json.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
// handling of -d curl parameter is here.
$param = array(
'a' => 'cache_lvl',
'tkn' => '8afbe6dea02407989af4dd4c97bb6e25',
'email' => 'sample#example.com',
'z' => 'ex'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
$result = curl_exec($ch);
curl_close($ch);
print $result;
I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:
cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=#-;type=application/json' https://my.url.here
However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:
$post = array(
"exchangeInstance" => $json_string,
"type" => "application/json",
);
$url = 'myurlhere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($post);
var_dump($result);
echo $result;
var_dump($info);
As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.
However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?
curl -F !== -d
$post = array(
"exchangeInstance" => sprintf('#%s;type=application/json', $json_string),
);