I have searched a lot, and found many related questions and forums related to this, but this one is a challenging one.
I'm trying to POST a complex array via curl. It has to be form-data while the first value in the array is of type JSON.
The two other values of array are two images which are uploaded and ready to send.
I tried to run it in Postman, and works perfectly fine. I used the generated PHP code from Postman, but it is not working. Seems like postman is handling some of its tricks without revealing them to us.
Any way, I'm posting a Postman image to illustrate what I mean:
As you can see, I'm sending the data in form-data tab, my first value (param1) is a JSON with content-type application/json, the second and third values are images uploaded in Postman.
This works just fine in Postman.
The problem is, if I set Content-Type:multipart/form-data in header, the destination server throws an error saying the content-type must be JSON.
If I set the Content-Type:application/json in header, the destination server says content must be of type Multipart.
Somehow, I need to set both content-types. The main one as form-data and the one for param1 as JSON.
I paste the Postman code as well, may that be a good start for you fellas to help out with the code.
Postman Code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://xxxxx.com/xxxx/xxx/xxxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('param1' => '{
"AgentId":"1414",
"ContractId":36529,
"Files":[
{
"FileName":"car_card_front_image.png",
"FileTypeId":2
},
{
"FileName":"car_card_back_image.png",
"FileTypeId":2
}
]
}','param2'=> new CURLFILE('/C:/images/icons/car_card_back_image.png'),'param3'=> new CURLFILE('/C:/images/icons/car_card_front_image.png')),
CURLOPT_HTTPHEADER => array(
'authenticationToken: xxxx-xxx-xx-xxxxxxxx'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The PHP generated code by postman, is not working. One of the reasons can be that there's no content-type mentioned in it.
I tried modifying the code, adding content-types in header and in parameter, nothing seems to work.
If Postman can do it, we should be able it too, right?
Go ahead, make as much changes as you would or suggest anything that comes to your mind, I will test them all.
Cheeeeers...
May i suggest the ixudrra/curl library ?
It would make your life easier ....
$response = Curl::to('http://example.org')
->withData( array( 'Foo' => 'Bar' ) )
->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
->post();
Related
Script in Question: http://samples.geekality.net/image-fetcher/
You can click "view source" at the bottom to pull up the exact PHP and Javascript being used.
I used part of this script to extract images on a site of mine from another on separate domains and servers. I had to go to the one being scraped and add a CORS header "Access-Control-Allow-Origin" to allow my specific domain to do the AJAX $.post and actually receive data.
The script above does not require any site to have the CORS header for security. As far as I can tell I'm doing nothing different.
I have an HTML form then I'm grabbing the field data with jQuery and doing a $.post where the action is a PHP file that uses DomDocument to grab the HTML. The PHP then sorts through the data and echoes a JSON object. Then the jQuery sorts it all out and displays it on the page. Same thing they're doing.
I can't see how they're getting around the need for a "Access-Control-Allow-Origin" header on the site they're grabbing images from?
Thanks for your time and energy in this!
I think it's the combination of curl options in /image_finder.class.php
Specifically, the follow location, as far as I can tell.
curl_setopt_array($request, array
(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_CAINFO => 'cacert.pem',
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
));
I need to interface with a REST webserver. When I issue a request, it responds (correctly) with HTTP response code 201 (Created), and the relevant information (JSON object) in the body.
I can verify it using cURL.
I'm trying to get the body it via PHP's file_get_contents function, specifying a correct context object, with the following code:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'timeout' => 10.0,
'content' => $body,
'ignore_errors' => true
)
)
);
$answer = file_get_contents($url, false, $context);
However the function always returns a false value and I cannot get the returned body.
What can I do? (I'd rather not call cURL from PHP)
OK, I've found it.
The cause was an old php version,
that did'nt handle the 'ignore_errors' parameter.
Switching to a more recent version of php, now it works.
I'm trying to scrape the below feed (with permission) via PHP cURL:
http://www.safc.com/Home/RSS Feeds/News%20Feed
Loads fine in a browser, but gives me a 400 'bad request' with cURL.
$ch = curl_init($uri); //http://www.safc.com/Home/RSS Feeds/News%20Feed
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => CURL_CONNECT_TIMEOUT,
CURLOPT_USERAGENT => CURL_USER_AGENT,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true
));
$ret = curl_exec($ch);
Result is a 400; I know this from looking in curl_getinfo().
CURL_USER_AGENT is an arbitrary identifier as I realised some other feeds wouldn't spit out content unless this header was present. I have tried removing the headers one by one, and tried adding a few more, but that approach feels a bit needle/haystack.
Before I approach the owners of the site, does anyone know how I might resolve this?
use http://www.safc.com/home/rss%20feeds/news%20feed check different between "Home" and "home" there is 301 redirect when you use "Home".
I am going to send a Log in request to the server by using CURL in my PHP code. This request is POST and I wrote following code,
if (strpos($header, 'POST') !== false){
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'proxy',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postParameters,
CURLOPT_COOKIE => $postCoockies
));
$respond = curl_exec($curl);
curl_close($curl); }
and if I want to say what my variables are here:
$url is the exact String after POST and before HTTP/1.1.
when I want to send the GET request also, I am using this part as my urs which would be contained all parameters in my query String.
$postParameters is the exact String that exist in POST body.
(That's a String, not an associative array)
$postCoockies in the exact String after Cookie: in my request header.
(again, that's not an associative array)
My problem is:
When I send this request, server sends me back a response to only the URL I am passing. Which is to load the login page again! it seems that the server is not receiving my parameters!
Also, when I used GET request for login (I have access to the source code of website), and sent the whole URL and parameters inside the $url, the same thing happened.
Am I wrong somewhere in sending my request?
Should I consider something else here?
I'm currently building a project based on the Parse.com backend that includes uploading files.
Users can upload files and then access a list of these/download them, this all works fine.
However, I'm not sure how to implement the command to delete an upload. From the Parse.com forums as well as the Parse support document, the call is:
curl -X DELETE \
-H "X-Parse-Application-Id: <YOUR_APPLICATION_ID>" \
-H "X-Parse-Master-Key: <YOUR_MASTER_KEY>" \
https://api.parse.com/1/files/<FILE_NAME>
I've had a bit of a look online but the only curl commands I can find to execute commands is curl_setopt. I imagine the above needs to be converted, can anybody help with this or point me in the right direction?
So basically I need to be able to press a button on a website (through PHP) and have it run the above command.
Thanks in advance
According to given info you have to set custom request method 'DELETE' (by CURLOPT_CUSTOMREQUEST option) as well as custom headers (by CURLOPT_HTTPHEADER option).
So the code should look like this:
$options = array(
CURLOPT_NOBODY => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => array(
'X-Parse-Application-Id: <YOUR_APPLICATION_ID>',
'X-Parse-Master-Key: <YOUR_MASTER_KEY',
),
CURLOPT_URL => 'https://api.parse.com/1/files/<FILE_NAME>',
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
echo $response;
If it's useful I made a simple class to handle the api calls request via Curl
https://github.com/niklongstone/php-api