I'm trying to work off the Harvest php API example from the GitHub page.
Here is the code, but there is no output no matter what I try to do. If I use the Chrome Postman app I can retrieve data. I'm not sure what I'm doing wrong. Confidential information was removed. Any help is much appreciated.
<?php
$url = "https://company.harvestapp.com/?daily=";
$temp = getURL($url);
echo $temp;
function getURL($url) {
$credentials = "<email>:<password>";
$headers = array(
"Content-Type: application/xml",
"Accept: application/xml",
"Authorization-Basic: " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "test app site");
$data = curl_exec($ch);
curl_close($ch);
if ($data !=false){
return $data;
}
else
return "no data";
}
?>
This is the error I get from the curl_error function:
HTTP/1.1 401 Unauthorized Server: nginx Date: Fri, 09 May 2014 22:59:27 GMT Content-Type: application/xml; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Status: 401 Unauthorized Cache-Control: private, no-store, no-cache, max-age=0, must-revalidate X-Frame-Options: SAMEORIGIN X-Served-From: https://.harvestapp.com/ X-UA-Compatible: IE=Edge,chrome=1 Set-Cookie: _harvest_sess=; domain=.harvestapp.com; path=/; secure; HttpOnly X-Request-Id: e9d6eb74-5c73-430e-9854-ebf6f1c527c8 X-Runtime: 0.019223 You must be authenticated with Harvest to complete this request.
Related
iam using PHP CURL to send POSTFIELD but i Getting Error 415 Unsupported Media Type
Here it's my code :
$data = '<?xml version="1.0" encoding="euc-kr" ?>
<Product>
<selMnbdNckNm>Catenzo YI 099</selMnbdNckNm>
<selMthdCd>01</selMthdCd>
<dispCtgrNo></dispCtgrNo>
<prdAttrCd></prdAttrCd>
<dispCtgrNo></dispCtgrNo>
<prdAttrVal><prdAttrVal>
<prdNm></prdNm>
<prdStatCd></prdStatCd>
<prdWght></prdWght>
<dlvGrntYn></dlvGrntYn>
<minorSelCnYn></minorSelCnYn>
<suplDtyfrPrdClfCd></suplDtyfrPrdClfCd>
<prdImage01></prdImage01>
<prdImage02></prdImage02>
<prdImage03></prdImage03>
<prdImage04></prdImage04>
<prdImage05></prdImage05>
<htmlDetail></htmlDetail>
<selTermUseYn></selTermUseYn>
<selPrc></selPrc>
<prdSelQty></prdSelQty>
<asDetail></asDetail>
<rtngExchDetail></rtngExchDetail>
</Product>';
$data = simplexml_load_string($data);
$ch = curl_init();
$header = array(
"Content-Type: application/xml",
"Accept-Charset: utf-8",
"openapikey:myapikey",
'Content-Length: ' . strlen($data)
);
curl_setopt($ch, CURLOPT_URL, "http://dev.api.elevenia.co.id/rest/prodservices/product");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$return=curl_exec($ch);
echo "[MSG] Result -Xml : \n";
echo $return;
I getting error message
HTTP/1.1 415 Unsupported Media Type Date: Wed, 03 Jan 2018 18:19:00 GMT Server: Apache Cache-Control: no-cache Cache-Control: no-store Pragma: no-cache Content-Length: 903 Expires: Thu, 01 Jan 1970 00:00:00 GMT Set-Cookie: WMONID=RgWtbnOnqnT; expires=Thu, 03-Jan-2019 18:19:00 GMT; path=/ X-Powered-By: Servlet/2.5 JSP/2.1 Vary: User-Agent Content-Type: text/html; charset=UTF-8
I hope someone can help me.
Thank You for #astrangeloop and #frz3993 i just have small error with change $header to $headers, and the xml closing tag should be
I'm using Shopify API with PHP CURL POST Method to create a product. Kindly find the following code and the response. I got error "Not Found".
<?php
$products_array = array(
"product"=>array(
'title'=>'',
"title"=> "Burton Custom Freestlye 151",
"body_html"=> "<strong>Good snowboard!</strong>",
"vendor"=> "Burton",
"product_type"=> "Snowboard",
"published"=> false ,
"variants"=>array(
array(
"sku"=>"t_009",
"price"=>20.00,
"grams"=>200,
"taxable"=>false,
)
)
)
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password#hostname/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
?>
The response of the request :
{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 01 Apr 2017 21:08:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block; report=/xss- report?source%5Baction%5D=create&source%5Bapp%5D=Shopify& source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin&source%5Buuid%5D=5539f94e-776f-4672-ab2a-f340c0cf2ad1
X-Dc: ash,chi2
X-Request-ID: 5539f94e-776f-4672-ab2a-f340c0cf2ad1
{"errors":"Not Found"}
How can I fix that error ? and what does that mean ??
Double check that you are using the right .myshopify.com hostname for your store. You can see this (along with the API key and password) after creating a private app in the Shopify admin: https://shopify.com/admin/apps/private
I'm trying to scrape a page with the use of cURL but all of my attempts doesn't work.
Here's my code:
PHP
public function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$loc = null;
if(preg_match('#Location: (.*)#', $data, $r)) {
$loc = trim($r[1]);
}
echo "<pre>";
echo var_dump($data);
echo "</pre>";
echo "<pre>";
echo var_dump($loc);
echo "</pre>";
die();
return $data;
}
The response I get by running that is the following:
HTTP/1.1 503 Service Temporarily Unavailable
Date: Wed, 28 Dec 2016 20:29:28 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
Set-Cookie: __cfduid=d6f3effa0b8c33cd8092e9f003d5c751c1482956968; expires=Thu, 28-Dec-17 20:29:28 GMT; path=/; domain=.thedomaintoscrape.com; HttpOnly
X-Frame-Options: SAMEORIGIN
Refresh: 8;URL=/cdn-cgi/l/chk_jschl?pass=1482956972.162-3LFzqX3Gdh
Cache-Control: no-cache
Server: cloudflare-nginx
CF-RAY: 3187c3bb054a551c-ORD
I don't really know what to make of it as I don't understand what the problem is. Can anyone help me?
I am trying to download a file with cURL. I have received an URL where the file is located, but the url makes an redirect before reaching the file. For some reason I always receive the logout page when I access the URL with cURL, but when I enter the URL directly in my browser the file just downloads as it is supposed to. The file that should be downloaded is a RAR file, but instead of the file I get the incorrect login page.
This the current code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
As you can see I am using the following code to allow the redirects:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
But I always receive the incorrect login page from the website if I use the above code. Can anyone see what I am doing wrong here ?
This is the reponse I get from the server:
HTTP/1.1 302 Found
Date: Tue, 26 Jan 2016 15:33:18 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.3.2
Set-Cookie: PHPSESSID=session_id; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /nl/nl/export/download/t/Mg==/c/MTQ=/
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Try with this code
$url = "www.abc.com/xyz";//your url will come here
$fp = fopen ('test.txt', 'w+');
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
I am using Gcm for sending notifications and I have used curl for the same. Here is the script
$registrationId= array('adkbvkasdjb');
$headers=array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$sendData=array(
'msg'=>'any message');
$url= GCM_URL;
$details=array(
'registration_ids'=>$registrationId,
'time_to_live'=> 48*60*60,
'data'=>$sendData,
);
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($details));
$result= curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
// closing the curl connection
curl_close($ch);
Now i am getting the header of the response as
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Thu, 16 Jan 2014 11:05:17 GMT
Expires: Thu, 16 Jan 2014 11:05:17 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
Since I am not very good with regex and i searched a lot about a method that could parse the header of the response but all in vain.
Question:-
So i just wanna know is there anything or any method that could parse the header response as an array ?
Note:- I found a little about http_parse_header() but it need pcre to be installed so i am not sure whether it is installed on my REAL SERVER :)
You don't need any Regex to do it. If you read the first comment of parse_http_headers documentation on php.net, there is a little implementation if pecl_http lib is not found.
Follow the link:
http://www.php.net/manual/en/function.http-parse-headers.php#112986