I am trying to use CURL to post the following fields to PANDADOCS, but for some reason I am getting an error that the values are not being received on their side.
This is the error I am getting:
"type": "validation_error", "detail": {"url": ["This field is required."], "name": ["This field is required."]}}
I am posting with the following:
$docurl = "myurl.com/document.pdf";
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-Type: application/json;charset=UTF-8';
$headr[] = "Authorization: Bearer $ACCESS_TOKEN";
$url = 'https://api.pandadoc.com/public/v1/documents';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$postfields = array();
$postfields['name'] = 'PSA';
$postfields['url'] = $docurl;
$postfields['recipients'] = array ([0]=>array(
['email'] => ['dondon#gmail.com'],
['first_name'] => ['don'],
['last_name'] => ['jones'],
['role']=>['u1'] ));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $postfields) );
$ret = curl_exec($ch); //
when I print_r($postfields)
I get
Array ( [name] => PSA [url] => https://api.pandadoc.com/public/v1/documents [recipients] => Array ( ) )
so all the fields arent getting posted.
but whats wierd is that the URL and NAME are in the array but not the other fields yet the error is complaining about not receiving NAME and URL..
dazed and confused...
* Hostname was found in DNS cache
* Trying 54.190.72.92...
* Connected to api.pandadoc.com (54.190.72.92) port 443 (#28)
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
* subject: OU=GT83522468; OU=See www.rapidssl.com/resources/cps (c)14; OU=Domain Control Validated - RapidSSL(R); CN=*.pandadoc.com
* start date: 2014-11-09 00:32:24 GMT
* expire date: 2016-10-11 09:34:58 GMT
* subjectAltName: api.pandadoc.com matched
* issuer: C=US; O=GeoTrust Inc.; CN=RapidSSL SHA256 CA - G3
* SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
> POST /public/v1/documents HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2
Host: api.pandadoc.com
Accept: */*
Content-length: 0
Content-Type: application/json;charset=UTF-8
Authorization: Bearer [ACCESS TOKEN]
* upload completely sent off: 37 out of 37 bytes
< HTTP/1.1 400 BAD REQUEST
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Fri, 06 Mar 2015 19:52:53 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept
< Allow: GET, POST, DELETE, HEAD, OPTIONS
<
* Connection #28 to host api.pandadoc.com left intact
$string is not defined.
Add
$string = http_build_query( $postfields );
after
$postfields = array();
$postfields['name'] = 'PSA';
$postfields['url'] = $docurl;
// This is invalid array
$postfields['recipients'] = array ([0]=>array(
['email'] => ['dondon#gmail.com'],
['first_name'] => ['don'],
['last_name'] => ['jones'],
['role']=>['u1'] ));
http://php.net/manual/en/function.http-build-query.php
UPDATE
I just read Pandadoc API. They accept json data and your data was invalid. Also content type.
This should work:
<?php
$url = 'https://api.pandadoc.com/public/v1/documents';
$docurl = "myurl.com/document.pdf";
$postfields = array();
$postfields['name'] = 'PSA';
$postfields['url'] = $docurl;
$postfields['recipients'] = array(
array(
'email' => 'dondon#gmail.com',
'first_name' => 'don',
'last_name' => 'jones',
'role' => 'u1'
)
);
$data_string = json_encode( $postfields );
$headr = array();
$headr[] = 'Content-length: '.strlen( $data_string );
$headr[] = 'Content-type: application/json';
$headr[] = "Authorization: Bearer $ACCESS_TOKEN";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headr );
$result = curl_exec( $ch );
?>
Related
Below is my code to add contact to the mailchimp. I am getting mailchimp api key and list id from the env file in laravel. Also $data involves all the input.
$apiKey = env('MAILCHIMP_APIKEY');
$listId = env('MAILCHIMP_LIST_ID');
$auth = base64_encode( 'user:'.$apiKey);
$mailChimpdata = array(
'apikey' => $apiKey,
'email_address' => $data['email'],
'status' => $data['status'],
'merge_fields' => array(
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname']
)
);
$json_data = json_encode($mailChimpdata);
$ch = curl_init();
$memberId = md5(strtolower($data['email']));
$url = 'https://us19.admin.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
But I am getting this below error
HTTP/1.0 501 Not Implemented
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 334
Expires: Wed, 07 Apr 2021 08:49:45 GMT
Date: Wed, 07 Apr 2021 08:49:45 GMT
Connection: close
Set-Cookie: _abck=471ED28A26F6757CE57AC9E4318E7BD6~-1~YAAQHdcLF/keKqt4AQAASPOFqwWS/EeAKXso+2igviRTfIMw3pqbDMndhfZPfoX+eQn3Iv8iqfbogtNFmkU4lRDRQLdAWAYBhq2oOhDTiK ▶
Set-Cookie: bm_sz=5CBD698E96AC9E2B62898990FF45E196~YAAQHdcLF/oeKqt4AQAASPOFqwsLVm8fKmxtToeWU99vCUWske+XMuRtEjiCpzXhPE5xe8jGoh6EfYe8WG6zjMaaI2jPZos0gb2ER9jykveOE ▶
<HTML><HEAD>
<TITLE>Unsupported Request</TITLE>
</HEAD><BODY>
<H1>Unsupported Request</H1>
PUT to http://us19.admin.mailchimp.com/3.0/lists/applevendor/members/b642b4217b34b1e8d3bd915fc65c4452 not supported.<P>
Reference #8.1dd70b17.1617785385.68d39e
</BODY></HTML>
Please help me out to resolve this issue.
Based on Mailchimp documentation they use URL to add contact
"https://us20.api.mailchimp.com/3.0/lists/$list_id/members/"
but you have
"https://us19.admin.mailchimp.com/3.0/lists/$listId/members/$memberId"
with $memberId. I guess that id is their internal identificator.
I don't know if this topic is still relevant but here is my method using php / Guzzle:
$client = new Client();
$client->request('PUT', 'https://us9.api.mailchimp.com/3.0/lists/{list_id}/members/{subscriber_hash},
[
'json' => [
'email_address' => $email,
'status_if_new' => "unsubscribed",
],
'headers' => [
"Authorization" => "Basic YOUR_API_KEY"
]
]);
subscriber_hash => hash MD5
I have the method below that is to get the response from webserver using cURL.
function login (string $_login, string $_password) : string {
$url = "https://acweb.net.br/api/orcamentos/login";
$fields = [
"login" => $_login,
"password" => $_password
];
$headers = [
"Try" => "Trying"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec( $ch );
}
It works fine!
i can get the value of $_POST with
print_r ($_POST)
But i can't get the value of CURLOPT_HTTPHEADER.
EDIT:
I did try so:
print_r ($_SERVER)
but it wasn't there.
How can i get the value of CURLOPT_HTTPHEADER?
all HTTP_headers in $_SERVER:
[HTTP_HOST] => ctemcasb.com.br
[HTTP_CONNECTION] => keep-alive
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
[HTTP_SEC_FETCH_USER] => ?1
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
[HTTP_SEC_FETCH_SITE] => none
[HTTP_SEC_FETCH_MODE] => navigate
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br
[HTTP_ACCEPT_LANGUAGE] => pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
[HTTP_COOKIE] => PHPSESSID=j4cqqdc83fia68nk0gsglqk1bv
HTTP_TRY, not exists.
And now?
I did this in the server:
print_r($_SERVER)
and
print_r ($_SERVER["HTTP_TRY]);
The headers shouldn't be an associative array, it should be an indexed array of strings.
$headers = [
'Try: Trying',
'Content-Type: text/html',
...
];
Then you should be able to access the header with: $_SERVER['HTTP_TRY'] since custom headers are prefixed with HTTP_
Please before my question please see those data 1st.
API URL: https://api.awebsite.com/api/redeem
Data Sent Method: POST
Requested Headers:
Host: api.awebsite.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://m.awebsite.com/en/exchange
Content-Type: application/json
Content-Length: 87
Origin: https://m.awebsite.com
Connection: keep-alive
Cookie: PHPSESSID=e0c4f6ec8a13e963bf6b11ebc33a96d2
TE: Trailers
Pragma: no-cache
Cache-Control: no-cache
Posted Data
{"redeemcode":"f564hfkj4shfee25","gameid":"123456","vcode":"7895","language":"en"}
I collect all of those from Browser > Inspect > Network area.
My Question is, Can I use php curl to post data to that api url from my localhost or my server? I Write my own code but its not working.. Here is my code.
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://m.awebsite.com/en/exchange');
//Execute the request
$result = curl_exec($ch);
Do you think there is a way to post data using php?
Yes, you can send post with cURL to other domain but... the other domain (https://api.awebsite.com/api/redeem) need allow the access with a cross domian policy
<?PHP
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonDataEncoded,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYPEER => false // <= Skip the secure validation
);
curl_setopt_array($ch, ($defaults));
//Execute the request
echo $result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
curl_close($ch);
I'm trying to get some data from that website: https://stubhub.com .
1- With file_get_contents:
$url= 'https://www.stubhub.com';
$html = file_get_contents($url);
echo $html;
I get:
Warning: file_get_contents(https://stubhub.com): failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed
2- With CURL:
$url= 'https://www.stubhub.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$html = curl_exec($curl);
$response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($html);
var_dump($response);
But I get:
bool(false) int(0)
I tried to add some headers like User-Agent and proxy:
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201');
$proxy = '185.135.226.159:23500';
curl_setopt($curl, CURLOPT_PROXY, $proxy);
But again I get the same.
I have allow_url_fopen=On, So what's wrong?
function curl( $url=NULL, $options=NULL ){
$cacert='c:/wwwroot/cacert.pem'; # <----- download your own copy and configure this path
$vbh = fopen('php://temp', 'w+');
$res=array(
'response' => NULL,
'info' => array( 'http_code' => 100 ),
'headers' => NULL,
'errors' => NULL
);
if( is_null( $url ) ) return (object)$res;
session_write_close();
/* Initialise curl request object */
$curl=curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
/* Define standard options */
curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );
curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh );
/* Assign runtime parameters as options */
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}
/* Execute the request and store responses */
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );
return $res;
}
$url='https://www.stubhub.com/';
$res = curl( $url );
if( $res->info->http_code==200 ){
printf('<pre>%s</pre>',print_r( $res->info,true ));
printf('<pre>%s</pre>',print_r( $res->verbose,true ));
}
This will output:
stdClass Object
(
[url] => https://www.stubhub.com/
[content_type] => text/html
[http_code] => 200
[header_size] => 1304
[request_size] => 214
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.609
[namelookup_time] => 0.25
[connect_time] => 0.265
[pretransfer_time] => 0.39
[size_upload] => 0
[size_download] => 1194
[speed_download] => 1960
[speed_upload] => 0
[download_content_length] => 1194
[upload_content_length] => -1
[starttransfer_time] => 0.609
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 23.43.75.46
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 192.168.0.56
[local_port] => 5042
)
* Trying 23.43.75.46...
* TCP_NODELAY set
* Connected to www.stubhub.com (23.43.75.46) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
CAfile: c:/wwwroot/cacert.pem
CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=US; ST=California; L=San Francisco; O=Stubhub, Inc.; OU=Technology; CN=www.stubhub.com
* start date: Jun 11 00:00:00 2018 GMT
* expire date: Jan 9 12:00:00 2020 GMT
* subjectAltName: host "www.stubhub.com" matched cert's "www.stubhub.com"
* issuer: C=US; O=DigiCert Inc; CN=DigiCert ECC Secure Server CA
* SSL certificate verify ok.
> GET / HTTP/1.1
Host: www.stubhub.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept: */*
Accept-Encoding: deflate, gzip
< HTTP/1.1 200 OK
< Server: nginx
< Content-Type: text/html
< Expires: Thu, 01 Jan 1970 00:00:01 GMT
< Cache-Control: private, no-cache, no-store, must-revalidate
< Surrogate-Control: no-store, bypass-cache
< Content-Encoding: gzip
< X-EdgeConnect-MidMile-RTT: 163
< X-EdgeConnect-Origin-MEX-Latency: 24
< X-Akamai-Transformed: 9 624 0 pmb=mTOE,1mRUM,1
< Date: Sat, 20 Oct 2018 16:25:57 GMT
< Content-Length: 1194
< Connection: keep-alive
< Vary: Accept-Encoding
< Set-Cookie: DC=lvs31;Path=/;Domain=stubhub.com;Expires=Sat, 20-Oct-2018 16:55:56 GMT;Max-Age=1800
< Set-Cookie: akacd_PCF_Prod=1540053357~rv=98~id=53e183ee10a83152497c9102c8c7dee7; path=/; Expires=Sat, 20 Oct 2018 16:35:57 GMT
< Strict-Transport-Security: max-age=31536000; includeSubDomains
< Set-Cookie: _abck=10D08E1267D29C2EDBEA32445BD116805C7A3616AB3500001557CB5B9AD22713~-1~e+BGOJkoD/UwtPOWH75YXUSo6Kzyd7sF6nTkkw89JfE=~-1~-1; expires=Sun, 20 Oct 2019 16:25:57 GMT; max-age=31536000; path=/; domain=.stubhub.com
< Set-Cookie: bm_sz=7C06CFF7557E22DEC7855EC89DF628B0~QAAQFjZ6XGg5goBmAQAAIypMkhVJRZxwtVU8097T7Q8Z2TcGPZR0XRtAVFY3TBHGsR4EW51MqZlCAyk3cMPDJEmukVvLunM36/5Kn1gtoxarUtgkqBvlfudWZBJb2xc1rHdnMhdsAXoHWLaGt0NwROSXckDe48kkqu2Kw3suRgrWcqDlj7Y1akARK8OYnoa6; Domain=.stubhub.com; Path=/; Expires=Sat, 20 Oct 2018 20:25:56 GMT; Max-Age=14399; HttpOnly
<
* Connection #0 to host www.stubhub.com left intact
To access the actual response body you would process $res->response - load it into DOMDocument or whatever you intend to do... good luck
My first post here... I am handling cookies between get/post pair of requests.
Based on this other question:
share the same cookie between two website using PHP cURL extension
Is my approach correct?
<?php
//set POST variables
$referer = 'http://www.correios.com.br/encomendas/prazo/';
$url = 'http://www.correios.com.br/encomendas/prazo/prazo.cfm';
$fields = array(
'Altura' => '8',
'Comprimento' => '16',
'Formato' => '1',
'Largura' => '15',
'MaoPropria' => 'N',
'avisoRecebimento' => 'N',
'cepDestino' => '99999999',
'cepOrigem' => '99999999',
'data' => '02/12/2012',
'dataAtual' => '02/12/2012',
'embalagem' => '',
'peso' => '1',
'resposta paginaCorreios'=> '',
'servico' => '99999',
'valorD' => '',
'valorDeclarado'=> ''
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, '10');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
// Post 1
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, $referer);
$result = trim(curl_exec($ch));
//
$ch = curl_init($url);
//Post 2
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_REFERER, $referer);
//execute post
$result = curl_exec($ch);
//
//close connection
curl_close($ch);
echo($result);
?>
What I need is mimic this behavior:
Response Headers:
Content-Type text/html; charset=ISO-8859-1
Date Sun, 02 Dec 2012 20:13:08 GMT
Server Microsoft-IIS/7.5
Set-Cookie JSESSIONID=c6308c7edb989a54edef1b656119101c321b;path=/ CFGLOBALS=urltoken%3DCFID%23%3D33736896%26CFTOKEN%23%3D28355865%26jsessionid%23%3Dc6308c7edb989a54edef1b656119101c321b%23lastvisit%3D%7Bts%20%272012%2D12%2D02%2018%3A13%3A09%27%7D%23timecreated%3D%7Bts%20%272012%2D12%2D02%2002%3A13%3A29%27%7D%23hitcount%3D6%23cftoken%3D28355865%23cfid%3D33736896%23;expires=Tue, 25-Nov-2042 20:13:09 GMT;path=/
Transfer-Encoding chunked
Request Headers:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Connection keep-alive
Cookie CFID=33736896; CFTOKEN=28355865; CFGLOBALS=urltoken%3DCFID%23%3D33736896%26CFTOKEN%23%3D28355865%26jsessionid%23%3D983074629e3d7344ff534b12637b7f127163%23lastvisit%3D%7Bts%20%272012%2D12%2D02%2002%3A56%3A48%27%7D%23timecreated%3D%7Bts%20%272012%2D12%2D02%2002%3A13%3A29%27%7D%23hitcount%3D5%23cftoken%3D28355865%23cfid%3D33736896%23
Host www.correios.com.br
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20100101 Firefox/17.0
Reading from some others topics, I have learned about multi_exec:
PHP cURL multi_exec delay between requests
I mean, what is the correct approach for the this task?
Thanks in advance!
Renato