Executing multiple cURL requests - php

I am trying to sequentially execute multiple curl requests, but I am not really having any luck, here is my code thus far,
//upload photo
$file= 'photo.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
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);
$args_two = array(
'message' => 'This is a test post',
);
$ch_two = curl_init();
$url_two = 'https://graph.facebook.com/me/feed?access_token='.$token;
curl_setopt($ch_two, CURLOPT_URL, $url_two);
curl_setopt($ch_two, CURLOPT_HEADER, false);
curl_setopt($ch_two, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_two, CURLOPT_POST, true);
curl_setopt($ch_two, CURLOPT_POSTFIELDS, $args_two);
$data_two = curl_exec($ch_two);
This file gets called and then the two requests should be executed one after the other, but right now it's just not working.
Thanx in advance!

Try to use curl_close
Try to use the same curl resource for two requests
Check if is it true, that none of requests is send to servers

Related

Make a wp_remote_post when not logged in

Hi i'm developing a plugin which adds a new field to the order (WooCommerce). The field needs to make an ajax request to a file in my plugin, that file then needs to make a cURL request to another website (or wp_remote_post). But i'm experiencing difficulties when making the request.
I can't get the ordinary cURL to work nor the wp_remote_post function.
Here's a snippet of the cURL in my file which the ajax requests to.
<?php
$shipping_place = array(
'country_code' => $country_code,
'postcode' => $postcode,
'street' => $street,
'number_of_droppoints' => $number_of_droppoints
);
$auth = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode('user:password')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $shipping_place);
$result = curl_exec($ch);
if(curl_errno($ch)){
$msg = 'Curl error: ' . curl_error($ch);
} else {
$result = json_decode($result['body']);
if ( $result->status == 'error' ) {
echo $result;
}
pred($result);
echo $result->result;
}
curl_close ($ch);
?>
Solved: I had to localize the wp-admin script.
That would use the wp function like this
<?php wp_localize_script( $handle, $name, $data ); ?>
Reference here.

PHP curl post image from URL

Currently, to POST image from local folder I use curl and PHP, here is my code:
$url = 'http://myprestashop.com/api/images/products/1';
$image_path = 'C:\\my_image.png';
$key = 'My web service key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
How can I POST image from URL (http://imageserver.com/image.jpeg) instead of local folder (C:\my_image.png)? Is it possible?
Just change your image path and try this code.
$url = 'http://myprestashop.com/api/images/products/1';
$data = array('key' => 'My web service key', 'image_path' => '#/home/user/test.png');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

POST to Facebook page via CURL

I've set up a script to post news to my facebook page via PHP
It worked for the last 2 years
Now, without notice it stopped working.
I correctly get the access_token but the second part returns this error
{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1}}
Here is the code
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "client_id=KEY&client_secret=SECRET&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$access_token = str_replace( "access_token=", "", curl_exec($curl) );
$titolo = 'Test';
$link_pulito = 'test.html';
$testo_fb = 'Test';
$attachment = array(
'access_token' => $access_token,
'message' => 'MESSAGE',
'name' => 'test',
'link' => 'http://www.test.com/workshop/',
'description' => 'test test test',
'picture'=>'http://www.test.com/77818763a19937bdd82b25f26cef2522.jpg'
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/MYPAGE/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //to suppress the curl output
$result= curl_exec($ch);
curl_close ($ch);
Facebook has changed the way of leasing tokens.
Please use the facebook PHP SDK from https://github.com/facebook/facebook-php-sdk

curl POST error: The requested URL returned error: 413

I've looked around a bit and can't find a solution that solves my problem. I'm getting the following error: "The requested URL returned error: 413."
I'm posting some information via curl and PHP to a URL via HTTPS. I'm told that I have to pass the "Content-Length" along with my request since the destination is https and not http.
Here's the code I'm using:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$api = curl_exec($ch);
My strlen($data_string) command is returning a value of 5, which is much less than the actual length of the $data_string, which is much longer. I assume this is the problem unless someone thinks it might be being caused by something else.
Here's what I ended up with:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$api = curl_exec($ch);
No SSL or size errors and it seems to work fine.

Post Curl issue

<?php
$url = "http://website.com/folder/index.php";
$data = array('id' => 'R98s', 'name' => 'Bob', 'content' => 'Hello');
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
?>
This works great, only 1 problem though,
id like a way to get the content response from the posted data in a variable, and not show as if its the page.
Try the following:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE ); // return into a variable
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec( $ch ); // run!
curl_close($ch);
And never forget the curl_close($handle); at the end.
I always thought you needed this too
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($handle)

Categories